Skip to content

Commit

Permalink
Greatly speed up CopyToClipboard by having it accept a function to ge…
Browse files Browse the repository at this point in the history
…nerate its value.
  • Loading branch information
Zemnmez committed Apr 25, 2024
1 parent bd7a916 commit 653a7ff
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function Client() {
<li>
JSON{' '}
<CopyToClipboard
text={JSON.stringify(bw[Ok])}
text={() => JSON.stringify(bw[Ok])}
/>
<PrettyJSON value={bw[Ok]} />
</li>
Expand Down
5 changes: 3 additions & 2 deletions ts/factorio/react/blueprint.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { extent } from 'd3-array';
import { scaleLinear } from 'd3-scale';
import { z } from 'zod';
Expand Down Expand Up @@ -126,7 +127,7 @@ export function DisplayBlueprintBook({ book }: DisplayBlueprintBookProps) {
</ol>
) : null}

<CopyToClipboard text={MarshalBlueprintBookString(book)} />
<CopyToClipboard text={() => MarshalBlueprintBookString(book)} />
</article>
);
}
Expand All @@ -147,7 +148,7 @@ export function DisplayBlueprint({ blueprint }: DisplayBlueprintProps) {
</header>
) : null}
{blueprint.description ? <p>{blueprint.description}</p> : null}
<CopyToClipboard text={MarshalBlueprintString(blueprint)} />
<CopyToClipboard text={() => MarshalBlueprintString(blueprint)} />
<RenderBlueprint blueprint={blueprint} />
</article>
);
Expand Down
4 changes: 3 additions & 1 deletion ts/next.js/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export const DefaultContentSecurityPolicy: CspPolicy = {
'https://*.google-analytics.com',
'https://*.doubleclick.net',
]),
'require-trusted-types-for': new Set(["'script'"]),
...(process.env.NODE_ENV == 'development'
? {}
: { 'require-trusted-types-for': new Set(["'script'"]) }),
'script-src': new Set([
"'self'",
"'unsafe-inline'", // https://github.com/vercel/next.js/discussions/54907#discussioncomment-8178117
Expand Down
4 changes: 2 additions & 2 deletions ts/react/CopyToClipboard/CopyToClipboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { useMutation } from '@tanstack/react-query';
import { MouseEvent, useCallback } from 'react';

export interface CopyToClipboardProps {
readonly text: string;
readonly text: () => string;
}

export function CopyToClipboard(props: CopyToClipboardProps) {
const mutation = useMutation({
mutationFn: () => navigator.clipboard.writeText(props.text),
mutationFn: () => navigator.clipboard.writeText(props.text()),
});

const onClick = useCallback(
Expand Down

0 comments on commit 653a7ff

Please sign in to comment.