docusaurus/lib/core/GridBlock.js

103 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-07-07 13:28:29 -04:00
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
2017-07-07 13:28:29 -04:00
*/
2017-07-10 19:38:35 -04:00
const React = require("react");
const classNames = require("classnames");
2017-07-07 13:28:29 -04:00
2017-07-10 19:38:35 -04:00
const Marked = require("./Marked.js");
2017-07-07 13:28:29 -04:00
class GridBlock extends React.Component {
renderBlock(block) {
2017-07-10 19:38:35 -04:00
const blockClasses = classNames("blockElement", this.props.className, {
alignCenter: this.props.align === "center",
alignRight: this.props.align === "right",
fourByGridBlock: this.props.layout === "fourColumn",
imageAlignBottom: block.image && block.imageAlign === "bottom",
imageAlignSide:
block.image &&
(block.imageAlign === "left" || block.imageAlign === "right"),
imageAlignTop: block.image && block.imageAlign === "top",
threeByGridBlock: this.props.layout === "threeColumn",
twoByGridBlock: this.props.layout === "twoColumn"
2017-07-07 13:28:29 -04:00
});
2017-07-10 19:38:35 -04:00
const topLeftImage =
(block.imageAlign === "top" || block.imageAlign === "left") &&
this.renderBlockImage(block.image, block.imageLink);
2017-07-07 13:28:29 -04:00
2017-07-10 19:38:35 -04:00
const bottomRightImage =
(block.imageAlign === "bottom" || block.imageAlign === "right") &&
this.renderBlockImage(block.image, block.imageLink);
2017-07-07 13:28:29 -04:00
return (
<div className={blockClasses} key={block.title}>
{topLeftImage}
<div className="blockContent">
{this.renderBlockTitle(block.title)}
2017-07-10 19:38:35 -04:00
<Marked>
{block.content}
</Marked>
2017-07-07 13:28:29 -04:00
</div>
{bottomRightImage}
</div>
);
}
renderBlockImage(image, imageLink) {
2017-07-07 13:28:29 -04:00
if (image) {
if (imageLink) {
return (
<div className="blockImage">
<a href={imageLink}>
<img src={image} />
</a>
</div>
);
} else {
return (
<div className="blockImage">
<img src={image} />
</div>
);
}
2017-07-07 13:28:29 -04:00
} else {
return null;
}
}
renderBlockTitle(title) {
if (title) {
2017-07-10 19:38:35 -04:00
return (
<h2>
<Marked>
{title}
</Marked>
2017-07-10 19:38:35 -04:00
</h2>
);
2017-07-07 13:28:29 -04:00
} else {
return null;
}
}
render() {
return (
<div className="gridBlock">
{this.props.contents.map(this.renderBlock, this)}
</div>
);
}
}
GridBlock.defaultProps = {
2017-07-10 19:38:35 -04:00
align: "left",
2017-07-07 13:28:29 -04:00
contents: [],
2017-07-10 19:38:35 -04:00
imagealign: "top",
layout: "twoColumn"
2017-07-07 13:28:29 -04:00
};
module.exports = GridBlock;