-
Notifications
You must be signed in to change notification settings - Fork 3
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
6 changed files
with
130 additions
and
17 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/components/molecules/CollapseReact/CollapseReact.stories.tsx
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,54 @@ | ||
import React from 'react'; | ||
|
||
import CollapseReact, { Props } from './CollapseReact'; | ||
|
||
const doc = ` | ||
Collapse (react) is a React widget. To use it in your project, follow these steps: | ||
**1**. Include the styles in the \`<head>\`: | ||
\`\`\`html | ||
<link rel="stylesheet" href="styles.css"> | ||
\`\`\` | ||
**2**. Include React and the following bundle before \`<body>\` tag's closing: | ||
\`\`\`html | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js" ></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script> | ||
<script src="collapse-vanilla.bundle.js"></script> | ||
\`\`\` | ||
**3**. Use it like so: | ||
\`\`\`html | ||
<div class="sb-collapse" data-label="Click me"> | ||
[HTML content] | ||
</div> | ||
\`\`\` | ||
`; | ||
|
||
export default { | ||
title: 'Molecules/Collapse (react)', | ||
component: CollapseReact, | ||
parameters: { | ||
docs: { | ||
description: { | ||
component: doc, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const Template = (args: Props) => <CollapseReact {...args} />; | ||
|
||
export const Default = Template.bind({}); | ||
Default.args = { | ||
open: false, | ||
label: 'More content', | ||
children: ` | ||
<p class="py-3"> | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Non igitur potestis voluptate omnia dirigentes aut tueri aut retinere virtutem. Ergo, inquit, tibi Q. Duo Reges: constructio interrete. Ipse Epicurus fortasse redderet, ut [redacted]tus Peducaeus, [redacted]. Quae qui non vident, nihil umquam magnum ac cognitione dignum amaverunt. Illa videamus, quae a te de amicitia dicta sunt. An vero, inquit, quisquam potest probare, quod perceptfum, quod. | ||
</p> | ||
`, | ||
}; |
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,42 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
import Button from 'components/atoms/Button'; | ||
|
||
export type Props = { | ||
open?: boolean; | ||
label: string; | ||
children: string; | ||
}; | ||
|
||
const CollapseReact = ({ open, label, children }: Props): JSX.Element => { | ||
const [expanded, setExpanded] = useState(open); | ||
|
||
useEffect(() => setExpanded(open), [open]); | ||
|
||
return ( | ||
<> | ||
<Button | ||
primary | ||
aria-expanded={expanded} | ||
aria-controls="collapse-example" | ||
data-collapse-target="collapse-example" | ||
onClick={() => setExpanded(!expanded)} | ||
> | ||
{label} | ||
</Button> | ||
|
||
<div | ||
id="collapse-example" | ||
className={`overflow-hidden ${expanded ? 'h-auto' : 'h-0'}`} | ||
// eslint-disable-next-line react/no-danger | ||
dangerouslySetInnerHTML={{ | ||
__html: children, | ||
}} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
CollapseReact.defaultProps = {}; | ||
|
||
export default CollapseReact; |
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 @@ | ||
export { default } from './CollapseReact'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
import CollapseReact from 'components/molecules/CollapseReact'; | ||
import { Props } from 'components/molecules/CollapseReact/CollapseReact'; | ||
|
||
const roots = document.querySelectorAll('.sb-collapse'); | ||
roots.forEach(root => { | ||
if (root instanceof HTMLElement) { | ||
const data = root?.dataset as Props; | ||
const content = root?.innerHTML; | ||
ReactDOM.render(<CollapseReact {...data}>{content}</CollapseReact>, root); | ||
} | ||
}); | ||
|
||
// Can be used like so: | ||
// <head> | ||
// <link rel="stylesheet" href="styles.css"> | ||
// <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script> | ||
// <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script> | ||
// </head> | ||
// <body> | ||
// <div id="sb-button" data-size="small" data-label="My button"></div> <!-- bad idea ^^' --> | ||
// <script src="button.bundle.js"></script> | ||
// </body> | ||
// </html> |
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