-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start with react info page in storybook
- Loading branch information
Showing
5 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
apps/storybook/docs-components/CodeSnippet/CodeSnippet.module.css
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,39 @@ | ||
.codeSnippet { | ||
position: relative; | ||
max-width: 100%; | ||
overflow-x: auto; | ||
width: 100%; | ||
} | ||
|
||
.copyContainer { | ||
position: sticky; | ||
top: 0; | ||
left: 0; | ||
height: calc(100% - 1px); | ||
width: calc(100% - 1px); | ||
float: left; | ||
margin-right: -100%; | ||
pointer-events: none; | ||
} | ||
|
||
.icon { | ||
pointer-events: auto; | ||
position: absolute; | ||
top: 24px; | ||
right: 24px; | ||
height: 40px; | ||
width: 40px; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
background-color: #3b434d; | ||
color: white; | ||
border-radius: var(--ds-border-radius-md); | ||
border: none; | ||
transition: 0.15s all; | ||
cursor: pointer; | ||
} | ||
|
||
.icon:hover { | ||
background-color: #575e68; | ||
} |
95 changes: 95 additions & 0 deletions
95
apps/storybook/docs-components/CodeSnippet/CodeSnippet.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,95 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { FilesIcon } from '@navikt/aksel-icons'; | ||
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; | ||
import { format } from 'prettier/standalone.js'; | ||
import * as prettierMarkdown from 'prettier/plugins/markdown.js'; | ||
import * as prettierHtml from 'prettier/plugins/html.js'; | ||
import * as prettierCSS from 'prettier/plugins/postcss.js'; | ||
import * as prettierTypescript from 'prettier/plugins/typescript.js'; | ||
import * as prettierEstree from 'prettier/plugins/estree'; | ||
import { nightOwl } from 'react-syntax-highlighter/dist/cjs/styles/prism'; | ||
import { Tooltip } from '@digdir/designsystemet-react'; | ||
|
||
import classes from './CodeSnippet.module.css'; | ||
|
||
const plugins = [ | ||
prettierTypescript, | ||
prettierEstree, | ||
prettierCSS, | ||
prettierMarkdown, | ||
prettierHtml, | ||
]; | ||
|
||
type CodeSnippetProps = { | ||
language?: 'css' | 'html' | 'ts' | 'markdown'; | ||
children?: string; | ||
}; | ||
|
||
const CodeSnippet = ({ | ||
language = 'markdown', | ||
children = '', | ||
}: CodeSnippetProps) => { | ||
const [toolTipText, setToolTipText] = useState('Kopier'); | ||
const [snippet, setSnippet] = useState(''); | ||
|
||
useEffect(() => { | ||
async function formatSnippet( | ||
children: string, | ||
language: CodeSnippetProps['language'], | ||
) { | ||
try { | ||
const formatted = await format(children, { | ||
parser: language === 'ts' ? 'typescript' : language, | ||
plugins, | ||
}); | ||
setSnippet(formatted); | ||
} catch (error) { | ||
console.error('Failed formatting code snippet:', error); | ||
} | ||
} | ||
void formatSnippet(children, language); | ||
|
||
return () => { | ||
setSnippet(''); | ||
}; | ||
}, [children, language]); | ||
|
||
const onButtonClick = () => { | ||
setToolTipText('Kopiert!'); | ||
navigator.clipboard.writeText(children).catch((reason) => { | ||
throw Error(String(reason)); | ||
}); | ||
}; | ||
|
||
return ( | ||
<div className={classes.codeSnippet}> | ||
{snippet && ( | ||
<> | ||
<Tooltip content={toolTipText}> | ||
<button | ||
onMouseEnter={() => setToolTipText('Kopier')} | ||
onClick={() => onButtonClick()} | ||
className={classes.icon} | ||
title='Kopier' | ||
> | ||
<FilesIcon fontSize={20} /> | ||
</button> | ||
</Tooltip> | ||
<SyntaxHighlighter | ||
style={nightOwl} | ||
language='jsx' | ||
customStyle={{ | ||
fontSize: '15px', | ||
margin: 0, | ||
padding: '18px', | ||
}} | ||
> | ||
{snippet} | ||
</SyntaxHighlighter> | ||
</> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export { CodeSnippet }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Meta } from '@storybook/blocks'; | ||
|
||
import { CodeSnippet } from '@doc-components'; | ||
|
||
<Meta title='Bruk React' /> | ||
|
||
# Hvordan bruke React | ||
|
||
Introduksjon til bruk, regler og gotchas. | ||
|
||
## SSR | ||
|
||
Alle våre komponenter er klient komponenter, og bruker `"use client"`. | ||
|
||
Dersom du ønsker å bruke våre komponenter i dine server komponenter, så kan du ikke bruke dot-notation. | ||
Dette betyr at du ikke kan skrive `<Accordion.Item>`, men må skrive `<AccordionItem>`. Grunnen for dette er at serveren ikke kan få tak i noe | ||
som ligger i en klient komponent, som `Accordion` er. | ||
|
||
### I en server komponent: | ||
|
||
<CodeSnippet language='ts'> | ||
{` | ||
import { Accordion, AccordionHeader, AccordionItem } from '@digdir/designsystemet-react'; | ||
<Accordion> | ||
<AccordionItem> | ||
<AccordionHeader>...</AccordionHeader> | ||
... | ||
</AccordionItem> | ||
</Accordion> | ||
`} | ||
</CodeSnippet> | ||
|
||
### I en klient komponent: | ||
|
||
<CodeSnippet language='ts'> | ||
{` | ||
import { Accordion } from '@digdir/designsystemet-react'; | ||
<Accordion> | ||
<Accordion.Item> | ||
<Accordion.Header>...</Accordion.Header> | ||
... | ||
</Accordion.Item> | ||
</Accordion> | ||
`} | ||
</CodeSnippet> |