Skip to content

Commit

Permalink
Fix hydration warning on website (#7921)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Sep 13, 2022
1 parent 267d8c1 commit 7aac32c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 36 deletions.
21 changes: 13 additions & 8 deletions docs/components/content/CodeBox.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
/** @jsxRuntime classic */
/** @jsx jsx */
import type { HTMLAttributes } from 'react';
import { useToasts } from '@keystone-ui/toast';
import { HTMLAttributes, useEffect, useState } from 'react';
import { jsx } from '@emotion/react';
import copy from 'clipboard-copy';

import { CheckIcon } from '@keystone-ui/icons/icons/CheckIcon';
import { Copy } from '../icons/Copy';

type CodeBoxProps = {
code: string;
} & HTMLAttributes<HTMLElement>;

export function CodeBox({ code, ...props }: CodeBoxProps) {
const { addToast } = useToasts();
const [didJustCopy, setDidJustCopy] = useState(false);
useEffect(() => {
if (didJustCopy) {
const timeout = setTimeout(() => setDidJustCopy(false), 1000);
return () => clearTimeout(timeout);
}
}, [didJustCopy]);

const handleCopy = async () => {
try {
await copy(code);
addToast({ title: 'Copied to clipboard', tone: 'positive' });
} catch (e) {
addToast({ title: 'Failed to copy to clipboard', tone: 'negative' });
}
setDidJustCopy(true);
// we don't want to do anything if the copy fails
} catch {}
};

return (
Expand Down Expand Up @@ -60,7 +65,7 @@ export function CodeBox({ code, ...props }: CodeBoxProps) {
alignItems: 'center',
}}
>
<Copy css={{ height: '1.5rem' }} />
{didJustCopy ? <CheckIcon color="green" /> : <Copy css={{ height: '1.5rem' }} />}
</button>
</div>
);
Expand Down
21 changes: 1 addition & 20 deletions docs/components/docs/CopyToClipboard.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
/** @jsxRuntime classic */
/** @jsx jsx */
import { useToasts } from '@keystone-ui/toast';
import { jsx } from '@emotion/react';
import copy from 'clipboard-copy';

import { Link } from '../icons/Link';

export function CopyToClipboard({ value }: { value: string }) {
const { addToast } = useToasts();

const handleCopy = () => {
if (typeof value !== 'string' || typeof window === 'undefined') return;

const url = `${window.location.protocol}//${window.location.host}${window.location.pathname}`;
const text = `${url}#${value}`;

try {
copy(text);
addToast({ title: 'Copied to clipboard', tone: 'positive' });
} catch (e) {
addToast({ title: 'Failed to copy to clipboard', tone: 'negative' });
}
};

export function HeadingIdLink({ value }: { value: string }) {
return (
<a
href={`#${value}`}
Expand All @@ -48,7 +30,6 @@ export function CopyToClipboard({ value }: { value: string }) {
color: 'var(--link)',
},
}}
onClick={handleCopy}
>
<Link css={{ height: '1rem' }} />
</a>
Expand Down
6 changes: 3 additions & 3 deletions docs/components/docs/Heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import slugify from '@sindresorhus/slugify';
import { jsx } from '@emotion/react';
import { ReactNode } from 'react';

import { CopyToClipboard } from './CopyToClipboard';
import { HeadingIdLink } from './CopyToClipboard';

/*
* !THIS IS OLD. PLEASE USE THE Type COMPONENT INSTEAD!
Expand Down Expand Up @@ -58,7 +58,7 @@ type HeadingProps = BaseHeadingProps & {
};

export function Heading({ level, id, children, ...props }: HeadingProps) {
const hasCopy = level > 1 && level < 5;
const hasHeadingIdLink = level > 1 && level < 5;
const computedId = id === undefined ? getAnchor(children) : id;
const Tag = `h${level}` as const;
return (
Expand All @@ -84,7 +84,7 @@ export function Heading({ level, id, children, ...props }: HeadingProps) {
},
}}
>
{hasCopy && <CopyToClipboard value={computedId} />}
{hasHeadingIdLink && <HeadingIdLink value={computedId} />}
{children}
</span>
</Tag>
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@emotion/weak-memoize": "^0.3.0",
"@keystone-6/fields-document": "^4.1.0",
"@keystone-ui/core": "^5.0.1",
"@keystone-ui/toast": "^6.0.1",
"@keystone-ui/icons": "^6.0.1",
"@markdoc/markdoc": "^0.1.5",
"@mdx-js/loader": "next",
"@mdx-js/react": "next",
Expand Down
5 changes: 1 addition & 4 deletions docs/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/** @jsxRuntime classic */
/** @jsx jsx */
import { ToastProvider } from '@keystone-ui/toast';
import { jsx, Global, css, CacheProvider } from '@emotion/react';
import type { AppProps } from 'next/app';
import { cache } from '@emotion/css';
Expand Down Expand Up @@ -100,9 +99,7 @@ export default function App({ Component, pageProps }: AppProps) {
/>
</Head>
<SkipLinks />
<ToastProvider>
<Component {...pageProps} />
</ToastProvider>
<Component {...pageProps} />
</CacheProvider>
);
}

0 comments on commit 7aac32c

Please sign in to comment.