forked from mongodb/design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
169 additions
and
170 deletions.
There are no files selected for viewing
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { documentToReactComponents } from '@contentful/rich-text-react-renderer'; | ||
import { BLOCKS, INLINES } from '@contentful/rich-text-types'; | ||
import { | ||
Body, | ||
H1, | ||
H2, | ||
H3, | ||
Subtitle, | ||
Overline, | ||
Link, | ||
} from '@leafygreen-ui/typography'; | ||
import HeaderContent from './HeaderContent'; | ||
import renderAsset from './renderAsset'; | ||
import renderEntry from './renderEntry'; | ||
|
||
const ContentfulRichText = ({ document }) => ( | ||
<> | ||
{documentToReactComponents(document, { | ||
renderNode: { | ||
[BLOCKS.PARAGRAPH]: (node, children) => <Body>{children}</Body>, | ||
[BLOCKS.HEADING_1]: (node, children) => <H1><HeaderContent>{children}</HeaderContent></H1>, | ||
[BLOCKS.HEADING_2]: (node, children) => <H2><HeaderContent>{children}</HeaderContent></H2>, | ||
[BLOCKS.HEADING_3]: (node, children) => <H3><HeaderContent>{children}</HeaderContent></H3>, | ||
[BLOCKS.HEADING_4]: (node, children) => <Subtitle><HeaderContent>{children}</HeaderContent></Subtitle>, | ||
[BLOCKS.HEADING_5]: (node, children) => <Overline>{children}</Overline>, | ||
[BLOCKS.EMBEDDED_ASSET]: renderAsset, | ||
[BLOCKS.EMBEDDED_ENTRY]: renderEntry, | ||
[INLINES.HYPERLINK]: (node, children) => <Link href={node.data.uri} target="_blank">{children}</Link>, | ||
[INLINES.ASSET_HYPERLINK]: renderAsset, | ||
}, | ||
})} | ||
</> | ||
); | ||
|
||
export default ContentfulRichText; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import styled from '@emotion/styled'; | ||
import { palette } from '@leafygreen-ui/palette'; | ||
import kebabCase from 'lodash/kebabCase'; | ||
import { PropsWithChildren } from "react"; | ||
import Icon from '@leafygreen-ui/icon'; | ||
import Link from 'next/link'; | ||
|
||
const StyledAnchor = styled('a')` | ||
color: inherit; | ||
text-decoration: inherit; | ||
`; | ||
|
||
const LinkContent = styled('span')` | ||
position: relative; | ||
color: inherit; | ||
text-decoration: inherit; | ||
&:after { | ||
content: ''; | ||
position: absolute; | ||
width: 100%; | ||
height: 2px; | ||
bottom: -4px; | ||
left: 0; | ||
border-radius: 2px; | ||
} | ||
&:hover { | ||
&:after { | ||
background-color: ${palette.gray.light2}; | ||
} | ||
+ svg { | ||
opacity: 1; | ||
} | ||
} | ||
`; | ||
|
||
const StyledIcon = styled(Icon)` | ||
margin-left: 8px; | ||
opacity: 0; // overridden on LinkContent hover | ||
` | ||
|
||
const HeaderContent = ({ children }: PropsWithChildren<{}>) => { | ||
const headerId = kebabCase(children?.toString()); | ||
return ( | ||
<Link href={`#${headerId}`} passHref> | ||
<StyledAnchor id={headerId}> | ||
<LinkContent> | ||
{children} | ||
</LinkContent> | ||
<StyledIcon glyph="Link" fill={palette.gray.light1} /> | ||
</StyledAnchor> | ||
</Link> | ||
) | ||
} | ||
|
||
export default HeaderContent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import ContentfulRichText from "./ContentfulRichText"; | ||
|
||
export default ContentfulRichText; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Image from '../Image'; | ||
|
||
const renderAsset = node => { | ||
if (!node.data.target.fields) { | ||
return <>Invalid asset.</>; | ||
} | ||
|
||
const { title, file } = node.data.target.fields; | ||
const mimeType = file.contentType; | ||
const mimeGroup = mimeType.split('/')[0]; | ||
|
||
switch (mimeGroup) { | ||
case 'image': | ||
return <Image alt={title} src={file.url} width="100%" />; | ||
default: | ||
return <h1>Unsupported embedded-asset-block mimeGroup: ${mimeGroup!}</h1>; | ||
} | ||
}; | ||
|
||
export default renderAsset; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import ExpandableCard from '@leafygreen-ui/expandable-card'; | ||
|
||
const renderEntry = node => { | ||
const embeddedEntryNodeType = node.data.target?.sys?.contentType?.sys.id; | ||
const embeddedEntryFields = node.data.target.fields; | ||
|
||
switch (embeddedEntryNodeType) { | ||
case 'expandableCardBlock': { | ||
const { title, description, content } = embeddedEntryFields; | ||
return ( | ||
<ExpandableCard title={title} description={description}> | ||
<ContentfulRichText document={content} /> | ||
</ExpandableCard> | ||
); | ||
} | ||
default: | ||
return ( | ||
<h1> | ||
Unsupported embedded-entry-block nodeType: ${embeddedEntryNodeType!} | ||
</h1> | ||
); | ||
} | ||
}; | ||
|
||
export default renderEntry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.