|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import styled from 'styled-components'; |
| 4 | +import getTheme from '../../theme/getTheme'; |
| 5 | + |
| 6 | +// This wrapper is used for nothing other than filtering props that are passed to it so they don't |
| 7 | +// bleed through to the DOM |
| 8 | +// (styled-components does not filter props when wrapping a third-party component). |
| 9 | +const StyledMediaObject = styled.div``; |
| 10 | + |
| 11 | +/** |
| 12 | + * A layout utility for aligning a media figure next to content. |
| 13 | + * |
| 14 | + * ##### Inspiration |
| 15 | + * * https://getbootstrap.com/docs/4.0/layout/media-object/ |
| 16 | + * * https://lightningdesignsystem.com/utilities/media-objects/ |
| 17 | + * * https://philipwalton.github.io/solved-by-flexbox/demos/media-object/ |
| 18 | + * * http://www.stubbornella.org/content/2010/06/25/the-media-object-saves-hundreds-of-lines-of-code |
| 19 | + */ |
| 20 | +const MediaObject = styled(props => { |
| 21 | + const { renderLayout } = props; |
| 22 | + |
| 23 | + let content = renderLayout; |
| 24 | + if (typeof renderLayout === 'function') { |
| 25 | + content = renderLayout(props); |
| 26 | + } |
| 27 | + |
| 28 | + return <StyledMediaObject {...props}>{content}</StyledMediaObject>; |
| 29 | +})` |
| 30 | + ${getTheme('MediaObject')} |
| 31 | +`; |
| 32 | + |
| 33 | +MediaObject.Body = styled.div` |
| 34 | + ${getTheme('MediaObjectBody')} |
| 35 | +`; |
| 36 | +MediaObject.Media = styled.div` |
| 37 | + ${getTheme('MediaObjectMedia')} |
| 38 | +`; |
| 39 | + |
| 40 | +MediaObject.propTypes = { |
| 41 | + /** |
| 42 | + * How to align the media content with respect to the children content. |
| 43 | + */ |
| 44 | + align: PropTypes.oneOf(['top', 'center', 'bottom']), |
| 45 | + /** |
| 46 | + * The body content. |
| 47 | + */ |
| 48 | + children: PropTypes.node, |
| 49 | + /** |
| 50 | + * The spacing between media and children content. |
| 51 | + */ |
| 52 | + // eslint-disable-next-line zillow/react/forbid-prop-types |
| 53 | + gutter: PropTypes.any, |
| 54 | + /** |
| 55 | + * The media content. |
| 56 | + */ |
| 57 | + media: PropTypes.node, |
| 58 | + /** |
| 59 | + * You can use `renderLayout` to modify the default composition of the component. |
| 60 | + * Pass your own node using the `<MediaObject.Body>` and `<MediaObject.Media>` wrappers, |
| 61 | + * or pass a render function that receives enhanced props as the only argument. |
| 62 | + */ |
| 63 | + renderLayout: PropTypes.oneOfType([PropTypes.node, PropTypes.func]), |
| 64 | + /** |
| 65 | + * By default, the media will come before the body. |
| 66 | + * Set this to reverse the order of media and body. |
| 67 | + */ |
| 68 | + reverse: PropTypes.bool, |
| 69 | +}; |
| 70 | + |
| 71 | +MediaObject.defaultProps = { |
| 72 | + align: 'top', |
| 73 | + // eslint-disable-next-line zillow/react/prop-types |
| 74 | + renderLayout: ({ media, children }) => ( |
| 75 | + <React.Fragment> |
| 76 | + {media && <MediaObject.Media>{media}</MediaObject.Media>} |
| 77 | + {children && <MediaObject.Body>{children}</MediaObject.Body>} |
| 78 | + </React.Fragment> |
| 79 | + ), |
| 80 | + reverse: false, |
| 81 | +}; |
| 82 | + |
| 83 | +/** @component */ |
| 84 | +export default MediaObject; |
0 commit comments