Files
supabase/apps/www/components/MadeForDevelopers/CodeExamples.tsx
Francesco Sansalvadore b91532f6c7 Migrate to styling tokens (#18314)
* migrate some www components to tokens

* consolidate InteractiveShimmerCard to Panel component

* update tokens in blog

* update tokens in careers page

* update tokens in customers section

* update tokens in open-source section

* update tokens in Realtime page

* update tokens in Storage and Vector

* update tokens in SplitCodeBlockCarousel

* update tokens in PGCharts

* remove unused css files

* update tokens in Card

* update tokens in Pricing page

* clean up priving page imports

* remove hardcoded theme vars

* migrate first half of defaultTheme.ts to tokens

* migrate second half of defaultTheme.ts to tokens

* improve inputs

* add foreground to text-light and text-lighter

* add foreground to text-light and text-lighter

* migrate docs components with styling tokens

* migrate docs components with styling tokens

* fix broken Repos component

* fix broken classes in blog

* update tokens on Button and other components

* update tokens on IconPanel

* update studio main layout base styling tokens

* update tokens across studio, docs and www

* update tokens across studio, docs and www

* update ui/Panel to styling tokens

* update ExampleProject and TableEditorMenu tokens

* www vector page tokens

* update studio UI tokens

* update other studio UI tokens

* update more studio UI tokens

* change tokens here, change tokens there

* finish updating colors with tokens variables

* add gui sandbox for theme experimentation

* use common package for www, docs and studio and fix Command K tokens

* provide light mode default tokens options

* fix conflict leftover

* update loading line

* fix className typo

* fix prettier

* update themeSandbox preset default values

* fix text-background0

* prettier

* update warningBanner with warning color

* switch all border-border with border-default

* improve border-secondary and foreground-muted in light mode

* force ring color on toggle

* fix button bg color and border-muted light token

* fix input bg color

* fix dark button hover

* fix homepage product card

* fix code-hike table header colors

* button dark border

* remove tabIndex leftover from homepage main ctas

---------

Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>
Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
2023-11-07 03:40:53 +00:00

188 lines
5.3 KiB
TypeScript

import { useState } from 'react'
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter'
import js from 'react-syntax-highlighter/dist/cjs/languages/hljs/javascript'
import CopyToClipboard from 'react-copy-to-clipboard'
import {
createUserExample,
subscribeExample,
readExample,
createExample,
updateExample,
ExampleProps,
} from 'data/CodeExamples'
import monokaiCustomTheme from 'data/CodeEditorTheme'
import { Button, IconCopy, Space, Tabs } from 'ui'
SyntaxHighlighter.registerLanguage('javascript', js)
const ClipboardIcon = () => (
<svg
viewBox="0 0 24 24"
width="12"
height="12"
stroke="currentColor"
strokeWidth="2"
fill="none"
strokeLinecap="round"
strokeLinejoin="round"
className="css-i6dzq1"
>
<path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2" />
<rect x="8" y="2" width="8" height="4" rx="1" ry="1" />
</svg>
)
const CopiedIcon = () => (
<svg
viewBox="0 0 24 24"
width="12"
height="12"
stroke="currentColor"
strokeWidth="2"
fill="none"
strokeLinecap="round"
strokeLinejoin="round"
className="css-i6dzq1"
>
<polyline points="20 6 9 17 4 12" />
</svg>
)
const CodeExamples = () => {
const [example, setExample] = useState('createUserExample')
const [copied, setCopied] = useState(false)
type exampleListProps = {
[key: string]: ExampleProps
}
const exampleList: exampleListProps = {
createUserExample,
subscribeExample,
readExample,
createExample,
updateExample,
}
const lang = 'javascript'
function handleClick(key: string) {
setExample(key)
setCopied(false)
}
const Buttons = () => (
<Space direction="vertical" size={1}>
{Object.values(exampleList).map((x, i) => {
const length: number = Object.values(exampleList).length - 1
// const radiusStyles = {
// borderBottomRightRadius: i !== length ? '0' : undefined,
// borderBottomLeftRadius: i !== length ? '0' : undefined,
// borderTopLeftRadius: i !== length + 1 && i !== 0 ? '0' : undefined,
// borderTopRightRadius: i !== length + 1 && i !== 0 ? '0' : undefined,
// }
return (
<Button
block
type={'outline'}
size="small"
key={x.id}
onClick={() => handleClick(x.id)}
style={
example === x.id
? {
background: 'white',
color: '#3d3d3d',
// borderBottom: i !== 0 ? 'none' : 'red',
// ...radiusStyles,
}
: {
// borderBottom: i !== length ? 'none' : undefined,
// ...radiusStyles,
}
}
>
{x.name}
</Button>
)
})}
</Space>
)
const TabNav = () => (
<Tabs
key="mobile-tabs"
scrollable
onClick={(id: string) => handleClick(id)}
activeId={example}
type="underlined"
>
{Object.values(exampleList).map((x, i) => {
return (
<Tabs.Panel id={x.id} label={x.name} key={i}>
<span></span>
</Tabs.Panel>
)
})}
</Tabs>
)
return (
<div>
<div className="grid grid-cols-12 gap-2 xl:gap-8">
<div className="col-span-12 text-center lg:col-span-3 lg:hidden">{<TabNav />}</div>
<div className="col-span-12 lg:col-span-9">
<div className="bg-surface-100 rounded-md rounded-b-lg">
<div className="flex items-center justify-between p-2 pl-5">
<p className="text-dark-100 mr-2 truncate text-sm sm:text-base">
{exampleList[example].description}
</p>
<div className="dark">
<CopyToClipboard
text={exampleList[example].code[lang]}
onCopy={() => setCopied(true)}
>
<Button type="outline" icon={copied ? <CopiedIcon /> : <ClipboardIcon />}>
<span className="hidden sm:block">{copied ? 'Copied!' : 'Copy code'}</span>
</Button>
</CopyToClipboard>
</div>
</div>
<SyntaxHighlighter
language="javascript"
// @ts-ignore
style={monokaiCustomTheme}
className="rounded-b-lg"
customStyle={{
padding: 0,
fontSize: 12,
lineHeight: 1.2,
borderTop: '1px solid #393939',
background: '#181818',
}}
showLineNumbers
lineNumberContainerStyle={{
paddingTop: '128px',
}}
lineNumberStyle={{
minWidth: '48px',
background: '#1e1e1e',
paddingLeft: '21px',
color: '#828282',
fontSize: 12,
paddingTop: '4px',
paddingBottom: '4px',
}}
>
{exampleList[example].code[lang]}
</SyntaxHighlighter>
</div>
</div>
<div className="col-span-12 hidden text-center lg:col-span-3 lg:block">{<Buttons />}</div>
</div>
</div>
)
}
export default CodeExamples