docusaurus/lib/core/GridBlock.js

93 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-07-07 13:28:29 -04:00
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
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") &&
2017-07-07 13:28:29 -04:00
this.renderBlockImage(block.image);
2017-07-10 19:38:35 -04:00
const bottomRightImage =
(block.imageAlign === "bottom" || block.imageAlign === "right") &&
2017-07-07 13:28:29 -04:00
this.renderBlockImage(block.image);
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) {
if (image) {
return (
2017-07-10 19:38:35 -04:00
<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>
2017-07-20 19:15:04 -04:00
<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;