docusaurus/lib/core/GridBlock.js

100 lines
2.6 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
*/
const React = require('react');
const classNames = require('classnames');
2017-07-07 13:28:29 -04:00
const MarkdownBlock = require('./MarkdownBlock.js');
2017-07-07 13:28:29 -04:00
class GridBlock extends React.Component {
renderBlock(block) {
const blockClasses = classNames('blockElement', this.props.className, {
alignCenter: this.props.align === 'center',
alignRight: this.props.align === 'right',
fourByGridBlock: this.props.layout === 'fourColumn',
2017-07-10 19:38:35 -04:00
imageAlignSide:
block.image &&
(block.imageAlign === 'left' || block.imageAlign === 'right'),
imageAlignTop: block.image && block.imageAlign === 'top',
imageAlignRight: block.image && block.imageAlign === 'right',
imageAlignBottom: block.image && block.imageAlign === 'bottom',
imageAlignLeft: block.image && block.imageAlign === 'left',
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, block.imageAlt);
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, block.imageAlt);
2017-07-07 13:28:29 -04:00
return (
<div className={blockClasses} key={block.title}>
{topLeftImage}
<div className="blockContent">
{this.renderBlockTitle(block.title)}
<MarkdownBlock>{block.content}</MarkdownBlock>
2017-07-07 13:28:29 -04:00
</div>
{bottomRightImage}
</div>
);
}
renderBlockImage(image, imageLink, imageAlt) {
2017-07-07 13:28:29 -04:00
if (image) {
if (imageLink) {
return (
<div className="blockImage">
<a href={imageLink}>
<img src={image} alt={imageAlt} />
</a>
</div>
);
} else {
return (
<div className="blockImage">
<img src={image} alt={imageAlt} />
</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>
<MarkdownBlock>{title}</MarkdownBlock>
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 = {
align: 'left',
2017-07-07 13:28:29 -04:00
contents: [],
layout: 'twoColumn',
2017-07-07 13:28:29 -04:00
};
module.exports = GridBlock;