Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

add copy code functionality into code block example component

mildtomatoaad5a9f8c7cc
diff --git a/www/components/index_sections/CodeExamples.tsx b/www/components/index_sections/CodeExamples.tsx
index f31bea4e5ad..9fb1d334fe0 100644
--- a/www/components/index_sections/CodeExamples.tsx
+++ b/www/components/index_sections/CodeExamples.tsx
@@ -1,14 +1,18 @@
import { useState } from 'react'
+
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter'
import js from 'react-syntax-highlighter/dist/cjs/languages/hljs/javascript'
import rainbow from 'react-syntax-highlighter/dist/cjs/styles/hljs/rainbow'
+import { CopyToClipboard } from 'react-copy-to-clipboard'
+
import { examples } from './../../data/CodeExamples'
SyntaxHighlighter.registerLanguage('javascript', js)
const CodeExamples = () => {
const [example, setExample] = useState('subscribeExample')
+ const [copied, setCopied] = useState(false)
const exampleList = ['subscribeExample', 'readExample', 'createExample', 'updateExample']
@@ -16,6 +20,7 @@ const CodeExamples = () => {
function handleClick(key: string) {
setExample(key)
+ setCopied(false)
}
const buttons = exampleList.map((id) => {
@@ -23,7 +28,10 @@ const CodeExamples = () => {
<button
type="button"
onClick={() => handleClick(id)}
- className={"m-1 mb-4 rounded-md border border-gray-200 px-4 py-2 text-base font-medium text-gray focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-transparent sm:w-auto sm:text-sm" + (example === id ? " border-brand-100 bg-brand-100 text-brand" : " hover:bg-gray-200")}
+ className={
+ 'm-1 mb-4 rounded-md border border-gray-200 px-4 py-2 text-base font-medium text-gray focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-transparent sm:w-auto sm:text-sm' +
+ (example === id ? ' border-brand-100 bg-brand-100 text-brand' : ' hover:bg-gray-200')
+ }
>
{
// @ts-ignore
@@ -37,31 +45,31 @@ const CodeExamples = () => {
<div className="grid grid-cols-12 gap-2">
<div className="col-span-12 text-center">
{buttons}
- {/* <button
- type="button"
- className="mt-12 inline-flex items-center px-3 py-2 border border-transparent shadow-sm text-sm leading-4 font-medium rounded-md text-white bg-brand-600 hover:bg-brand-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-brand-500"
- >
- <svg
- className="-ml-0.5 mr-2 h-4 w-4"
- xmlns="http://www.w3.org/2000/svg"
- viewBox="0 0 20 20"
- fill="currentColor"
- aria-hidden="true"
- >
- <path d="M2.003 5.884L10 9.882l7.997-3.998A2 2 0 0016 4H4a2 2 0 00-1.997 1.884z" />
- <path d="M18 8.118l-8 4-8-4V14a2 2 0 002 2h12a2 2 0 002-2V8.118z" />
- </svg>
- Explore documentation
- </button> */}
</div>
<div className="col-span-8 col-start-3">
<div className="rounded" style={{ background: '#474949' }}>
- <p className="text-base text-gray-400 text-white p-2 pl-5">
+ <div className="p-2 pl-5 flex justify-between items-center">
+ <p className="text-base text-gray-400 text-white">
{
// @ts-ignore
examples[example].description
}
</p>
+ <CopyToClipboard
+ text={
+ // @ts-ignore
+ examples[example].code[lang]
+ }
+ onCopy={() => setCopied(true)}
+ >
+ <button
+ type="button"
+ className="inline-flex items-center px-2.5 py-1.5 border border-gray-500 shadow-sm text-xs font-medium rounded text-gray-300 hover:bg-gray-500 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
+ >
+ {copied ? 'Copied!' : 'Copy code'}
+ </button>
+ </CopyToClipboard>
+ </div>
<SyntaxHighlighter
// startingLineNumber={3}
language="javascript"
diff --git a/www/package-lock.json b/www/package-lock.json
index 9831d5fa4f6..4d787852ad2 100644
--- a/www/package-lock.json
+++ b/www/package-lock.json
@@ -1540,6 +1540,14 @@
"resolved": "https://registry.npmjs.org/copy-descriptor/-/copy-descriptor-0.1.1.tgz",
"integrity": "sha1-Z29us8OZl8LuGsOpJP1hJHSPV40="
},
+ "copy-to-clipboard": {
+ "version": "3.3.1",
+ "resolved": "https://registry.npmjs.org/copy-to-clipboard/-/copy-to-clipboard-3.3.1.tgz",
+ "integrity": "sha512-i13qo6kIHTTpCm8/Wup+0b1mVWETvu2kIMzKoK8FpkLkFxlt0znUAHcMzox+T8sPlqtZXq3CulEjQHsYiGFJUw==",
+ "requires": {
+ "toggle-selection": "^1.0.6"
+ }
+ },
"core-util-is": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
@@ -5658,6 +5666,15 @@
"prop-types": "^15.6.2"
}
},
+ "react-copy-to-clipboard": {
+ "version": "5.0.2",
+ "resolved": "https://registry.npmjs.org/react-copy-to-clipboard/-/react-copy-to-clipboard-5.0.2.tgz",
+ "integrity": "sha512-/2t5mLMMPuN5GmdXo6TebFa8IoFxZ+KTDDqYhcDm0PhkgEzSxVvIX26G20s1EB02A4h2UZgwtfymZ3lGJm0OLg==",
+ "requires": {
+ "copy-to-clipboard": "^3",
+ "prop-types": "^15.5.8"
+ }
+ },
"react-dom": {
"version": "16.14.0",
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-16.14.0.tgz",
@@ -6968,6 +6985,11 @@
"is-number": "^7.0.0"
}
},
+ "toggle-selection": {
+ "version": "1.0.6",
+ "resolved": "https://registry.npmjs.org/toggle-selection/-/toggle-selection-1.0.6.tgz",
+ "integrity": "sha1-bkWxJj8gF/oKzH2J14sVuL932jI="
+ },
"toidentifier": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz",
diff --git a/www/package.json b/www/package.json
index 04444759d28..4826ef8d1a2 100644
--- a/www/package.json
+++ b/www/package.json
@@ -15,6 +15,7 @@
"gray-matter": "4.0.2",
"next": "latest",
"react": "^16.13.0",
+ "react-copy-to-clipboard": "^5.0.2",
"react-dom": "^16.13.0",
"react-syntax-highlighter": "^15.3.1",
"remark": "11.0.2",