-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add button open in stackblitz (#4461)
* feat: add button open in stackblitz * feat: add button open in stackblitz * fix: multiple react * feat: add stackblitz button code preview * feat: add stackblitz button code preview
- Loading branch information
Showing
9 changed files
with
253 additions
and
31 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,43 +1,37 @@ | ||
import {FC} from "react"; | ||
import {Button, ButtonProps} from "@nextui-org/react"; | ||
import {ButtonProps} from "@nextui-org/react"; | ||
import {useClipboard} from "@nextui-org/use-clipboard"; | ||
import {clsx} from "@nextui-org/shared-utils"; | ||
import {memo} from "react"; | ||
|
||
import {PreviewButton} from "./preview-button"; | ||
|
||
import {CheckLinearIcon, CopyLinearIcon} from "@/components/icons"; | ||
|
||
export interface CopyButtonProps extends ButtonProps { | ||
value?: string; | ||
} | ||
|
||
export const CopyButton: FC<CopyButtonProps> = ({value, className, ...buttonProps}) => { | ||
export const CopyButton = memo<CopyButtonProps>(({value, className, ...buttonProps}) => { | ||
const {copy, copied} = useClipboard(); | ||
|
||
const icon = copied ? ( | ||
<CheckLinearIcon | ||
className="opacity-0 scale-50 data-[visible=true]:opacity-100 data-[visible=true]:scale-100 transition-transform-opacity" | ||
data-visible={copied} | ||
size={16} | ||
/> | ||
) : ( | ||
<CopyLinearIcon | ||
className="opacity-0 scale-50 data-[visible=true]:opacity-100 data-[visible=true]:scale-100 transition-transform-opacity" | ||
data-visible={!copied} | ||
size={16} | ||
/> | ||
); | ||
|
||
const handleCopy = () => { | ||
copy(value); | ||
}; | ||
|
||
return ( | ||
<Button | ||
isIconOnly | ||
className={clsx( | ||
"absolute z-50 right-3 text-zinc-300 top-8 border-1 border-transparent bg-transparent before:bg-white/10 before:content-[''] before:block before:z-[-1] before:absolute before:inset-0 before:backdrop-blur-md before:backdrop-saturate-100 before:rounded-lg", | ||
className, | ||
)} | ||
size="sm" | ||
variant="bordered" | ||
onPress={handleCopy} | ||
{...buttonProps} | ||
> | ||
<CheckLinearIcon | ||
className="absolute opacity-0 scale-50 data-[visible=true]:opacity-100 data-[visible=true]:scale-100 transition-transform-opacity" | ||
data-visible={copied} | ||
size={16} | ||
/> | ||
<CopyLinearIcon | ||
className="absolute opacity-0 scale-50 data-[visible=true]:opacity-100 data-[visible=true]:scale-100 transition-transform-opacity" | ||
data-visible={!copied} | ||
size={16} | ||
/> | ||
</Button> | ||
); | ||
}; | ||
return <PreviewButton className={className} icon={icon} onPress={handleCopy} {...buttonProps} />; | ||
}); | ||
|
||
CopyButton.displayName = "CopyButton"; |
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,31 @@ | ||
import {forwardRef} from "react"; | ||
import {Button, ButtonProps} from "@nextui-org/react"; | ||
import {clsx} from "@nextui-org/shared-utils"; | ||
|
||
export interface PreviewButtonProps extends ButtonProps { | ||
icon: React.ReactNode; | ||
} | ||
|
||
export const PreviewButton = forwardRef<HTMLButtonElement | null, PreviewButtonProps>( | ||
(props, ref) => { | ||
const {icon, className, ...buttonProps} = props; | ||
|
||
return ( | ||
<Button | ||
ref={ref} | ||
isIconOnly | ||
className={clsx( | ||
"relative z-50 text-zinc-300 top-8 border-1 border-transparent bg-transparent before:bg-white/10 before:content-[''] before:block before:z-[-1] before:absolute before:inset-0 before:backdrop-blur-md before:backdrop-saturate-100 before:rounded-lg", | ||
className, | ||
)} | ||
size="sm" | ||
variant="light" | ||
{...buttonProps} | ||
> | ||
{icon} | ||
</Button> | ||
); | ||
}, | ||
); | ||
|
||
PreviewButton.displayName = "PreviewButton"; |
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,58 @@ | ||
import React, {forwardRef} from "react"; | ||
import stackblitzSdk from "@stackblitz/sdk"; | ||
import {SandpackFiles} from "@codesandbox/sandpack-react/types"; | ||
|
||
import {StackblitzIcon} from "./icons"; | ||
|
||
import {useStackblitz} from "@/hooks/use-stackblitz"; | ||
import {Tooltip} from "@/../../packages/components/tooltip/src"; | ||
import {Button, ButtonProps} from "@/../../packages/components/button/src"; | ||
|
||
export interface StackblitzButtonProps extends ButtonProps { | ||
files: SandpackFiles; | ||
typescriptStrict?: boolean; | ||
className?: string; | ||
button?: React.ReactElement; | ||
icon?: React.ReactNode; | ||
} | ||
|
||
export const StackblitzButton = forwardRef<HTMLButtonElement, StackblitzButtonProps>( | ||
(props, ref) => { | ||
const { | ||
files, | ||
typescriptStrict = false, | ||
className, | ||
button = <Button />, | ||
icon = ( | ||
<StackblitzIcon | ||
data-visible | ||
className="opacity-0 data-[visible=true]:opacity-100 transition-transform-opacity" | ||
/> | ||
), | ||
...rest | ||
} = props; | ||
const {stackblitzPrefillConfig, entryFile} = useStackblitz({ | ||
files, | ||
typescriptStrict, | ||
}); | ||
|
||
return ( | ||
<Tooltip closeDelay={0} content="Open in Stackblitz"> | ||
{React.cloneElement(button, { | ||
ref, | ||
className, | ||
icon, | ||
onPress: () => { | ||
stackblitzSdk.openProject(stackblitzPrefillConfig, { | ||
openFile: [entryFile], | ||
}); | ||
}, | ||
children: icon, | ||
...rest, | ||
})} | ||
</Tooltip> | ||
); | ||
}, | ||
); | ||
|
||
StackblitzButton.displayName = "StackblitzButton"; |
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,88 @@ | ||
import {Project} from "@stackblitz/sdk"; | ||
import {SandpackFiles} from "@codesandbox/sandpack-react/types"; | ||
|
||
import {mapKeys, omit} from "@/../../packages/utilities/shared-utils/src"; | ||
import {useSandpack} from "@/components/sandpack/use-sandpack"; | ||
|
||
export interface UseSandpackProps { | ||
files: SandpackFiles; | ||
typescriptStrict?: boolean; | ||
} | ||
|
||
const viteConfig = `import { defineConfig } from "vite"; | ||
import react from "@vitejs/plugin-react"; | ||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [react()], | ||
}); | ||
`; | ||
|
||
function transformSandpackFiles(files: SandpackFiles) { | ||
return Object.fromEntries( | ||
Object.entries(files).map(([key, value]) => [ | ||
key, | ||
typeof value === "string" ? value : value.code, | ||
]), | ||
); | ||
} | ||
|
||
export function useStackblitz(props: UseSandpackProps) { | ||
const {files, typescriptStrict = false} = props; | ||
|
||
const { | ||
customSetup, | ||
files: filesData, | ||
entryFile, | ||
} = useSandpack({ | ||
files: transformSandpackFiles(files), | ||
typescriptStrict, | ||
}); | ||
|
||
const transformFiles = mapKeys(filesData, (_, key) => key.replace(/^\//, "")); | ||
|
||
const dependencies = {...customSetup.dependencies, ...customSetup.devDependencies}; | ||
|
||
const packageJson = `{ | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"preview": "vite preview" | ||
}, | ||
"dependencies": { | ||
"react": "18.3.1", | ||
"react-dom": "18.3.1", | ||
${Object.entries(omit(dependencies as any, ["react", "react-dom"])) | ||
.map(([key, value]) => `"${key}": "${value}"`) | ||
.join(",\n ")} | ||
}, | ||
"devDependencies": { | ||
"@vitejs/plugin-react": "4.3.4", | ||
"vite": "6.0.6", | ||
"autoprefixer": "10.4.20", | ||
"postcss": "8.4.49", | ||
"tailwindcss": "3.4.17" | ||
}, | ||
"main": "/index.jsx" | ||
}`; | ||
|
||
const stackblitzPrefillConfig: Project = { | ||
files: { | ||
...transformSandpackFiles(transformFiles), | ||
"vite.config.js": viteConfig, | ||
"package.json": packageJson, | ||
}, | ||
dependencies, | ||
title: "NextUI", | ||
template: "node", | ||
}; | ||
|
||
const findEntryFile = Object.keys(stackblitzPrefillConfig.files).find((key) => | ||
key.includes("App"), | ||
); | ||
|
||
return { | ||
entryFile: findEntryFile ?? entryFile, | ||
stackblitzPrefillConfig, | ||
}; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.