Skip to content

Commit

Permalink
Try a first version of Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
cdedreuille committed May 16, 2024
1 parent 2dbc498 commit 4fc4388
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const CodeSnippetsComponent: FC<CodeSnippetsClientProps> = ({

return (
<CodeWrapper
copy={activeContent?.content}
options={
<>
{filters && filters.languages.length > 1 ? (
Expand Down
10 changes: 8 additions & 2 deletions apps/frontpage/components/docs/mdx/code-snippets/copy.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
'use client';

import { CheckIcon, CopyIcon } from '@storybook/icons';
import { renderToStaticMarkup } from 'react-dom/server';
import type { ReactNode } from 'react';
import { useState } from 'react';
import copy from 'copy-to-clipboard';

export const Copy = () => {
export const Copy = ({ content }: { content: ReactNode }) => {
const [state, setState] = useState<'idle' | 'copied'>('idle');

const onClick = () => {
copy('Text Boom!');
const textToConvertIntoString = renderToStaticMarkup(content);
copy(textToConvertIntoString, {
debug: true,
// format: 'text/html',
});
setState('copied');
setTimeout(() => {
setState('idle');
Expand Down
12 changes: 7 additions & 5 deletions apps/frontpage/components/docs/mdx/code-snippets/wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import type { FC, ReactNode } from "react";
import { TSIcon } from "./icons";
import { Copy } from "./copy";
import type { FC, ReactNode } from 'react';
import { TSIcon } from './icons';
import { Copy } from './copy';

interface CodeWrapperProps {
options?: ReactNode;
children: ReactNode;
title?: string;
copy?: ReactNode;
}

export const CodeWrapper: FC<CodeWrapperProps> = ({
options,
children,
title,
copy,
}) => {
return (
<div className="border border-zinc-300 rounded overflow-hidden mb-6 w-full">
<div className="flex items-center justify-between py-2 pl-5 pr-4 border-b border-b-zinc-300 bg-slate-50">
<div className="flex items-center gap-2 font-bold text-sm">
<TSIcon /> {title || ""}
<TSIcon /> {title || ''}
</div>
<div className="flex items-center gap-2">
{options}
<Copy />
{copy ? <Copy content={copy} /> : null}
</div>
</div>
<div className="p-4 text-sm max-w-full overflow-scroll">{children}</div>
Expand Down
2 changes: 1 addition & 1 deletion apps/frontpage/components/docs/mdx/pre.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface PreProps {

export const Pre: FC<PreProps> = ({ children }) => {
return (
<CodeWrapper>
<CodeWrapper copy={children}>
<pre>{children}</pre>
</CodeWrapper>
);
Expand Down

0 comments on commit 4fc4388

Please sign in to comment.