|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import Input from './Input'; |
| 3 | +import CopyButton from './CopyButton'; |
| 4 | +import Embedded from './Embedded'; |
| 5 | +import { XIcon } from '@primer/octicons-react'; |
| 6 | + |
| 7 | +function TabButton({ children, active, onClick, disabled }) { |
| 8 | + return ( |
| 9 | + <button |
| 10 | + disabled={disabled} |
| 11 | + className={[ |
| 12 | + 'text-xs select-none border-b-2', |
| 13 | + disabled ? '' : 'hover:text-blue-400 hover:border-blue-400', |
| 14 | + active |
| 15 | + ? 'border-blue-600 text-blue-600' |
| 16 | + : 'border-transparent text-gray-800', |
| 17 | + ].join(' ')} |
| 18 | + onClick={disabled ? undefined : onClick} |
| 19 | + > |
| 20 | + {children} |
| 21 | + </button> |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +const possiblePanes = ['markup', 'preview', 'query', 'result']; |
| 26 | + |
| 27 | +function Embed({ dirty, gistId, gistVersion }) { |
| 28 | + const [panes, setPanes] = useState(['preview', 'result']); |
| 29 | + |
| 30 | + const width = 850; |
| 31 | + const height = 300; |
| 32 | + |
| 33 | + const embedUrl = |
| 34 | + [location.origin, 'embed', gistId, gistVersion].filter(Boolean).join('/') + |
| 35 | + `?panes=${panes.join(',')}`; |
| 36 | + |
| 37 | + const embedCode = `<iframe src="${embedUrl}" height="${height}" width="100%" scrolling="no" frameBorder="0" allowTransparency="true" title="Testing Playground" style="overflow: hidden; display: block; width: 100%"></iframe>`; |
| 38 | + const canAddPane = panes.length < 3; |
| 39 | + |
| 40 | + return ( |
| 41 | + <div className="settings text-sm pb-2"> |
| 42 | + <div className="space-y-6"> |
| 43 | + {dirty && ( |
| 44 | + <section className="bg-blue-100 p-2 text-xs rounded my-2 text-blue-800"> |
| 45 | + Please note that this playground has |
| 46 | + <strong> unsaved changes </strong>. The embed |
| 47 | + <strong> will not include </strong> your latest changes! |
| 48 | + </section> |
| 49 | + )} |
| 50 | + |
| 51 | + <section className="flex flex-col space-y-4"> |
| 52 | + <div className="flex items-center justify-between"> |
| 53 | + <h3 className="text-sm font-bold">Configure</h3> |
| 54 | + <TabButton |
| 55 | + onClick={() => |
| 56 | + setPanes([ |
| 57 | + ...panes, |
| 58 | + possiblePanes.find((x) => !panes.includes(x)), |
| 59 | + ]) |
| 60 | + } |
| 61 | + disabled={!canAddPane} |
| 62 | + > |
| 63 | + add pane |
| 64 | + </TabButton> |
| 65 | + </div> |
| 66 | + |
| 67 | + <div className="bg-gray-200 px-4 pb-4 -mx-4"> |
| 68 | + <div className="px-4 gap-4 grid grid-flow-col py-1"> |
| 69 | + {panes.map((selected, idx) => ( |
| 70 | + <div key={idx} className="flex items-center justify-between"> |
| 71 | + <div className="text-left space-x-2"> |
| 72 | + {possiblePanes.map((name) => ( |
| 73 | + <TabButton |
| 74 | + key={name} |
| 75 | + onClick={() => |
| 76 | + setPanes((current) => { |
| 77 | + const next = [...current]; |
| 78 | + next[idx] = name; |
| 79 | + return next; |
| 80 | + }) |
| 81 | + } |
| 82 | + active={selected === name} |
| 83 | + > |
| 84 | + {name} |
| 85 | + </TabButton> |
| 86 | + ))} |
| 87 | + </div> |
| 88 | + <TabButton |
| 89 | + disabled={panes.length === 1} |
| 90 | + onClick={() => setPanes(panes.filter((_, i) => i !== idx))} |
| 91 | + > |
| 92 | + <span> |
| 93 | + <XIcon size={12} /> |
| 94 | + </span> |
| 95 | + </TabButton> |
| 96 | + </div> |
| 97 | + ))} |
| 98 | + </div> |
| 99 | + |
| 100 | + <div style={{ width, height }}> |
| 101 | + <Embedded |
| 102 | + panes={panes} |
| 103 | + gistId={gistId} |
| 104 | + gistVersion={gistVersion} |
| 105 | + /> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + </section> |
| 109 | + |
| 110 | + <section className="flex flex-col space-y-4" style={{ width }}> |
| 111 | + <h3 className="text-sm font-bold">Copy & Paste</h3> |
| 112 | + |
| 113 | + <label className="text-xs"> |
| 114 | + embed link: |
| 115 | + <div className="flex space-x-4"> |
| 116 | + <Input value={embedUrl} onChange={() => {}} readOnly name="url" /> |
| 117 | + <CopyButton text={embedUrl} /> |
| 118 | + </div> |
| 119 | + </label> |
| 120 | + |
| 121 | + <label className="text-xs"> |
| 122 | + embed code: |
| 123 | + <div className="w-full flex space-x-4"> |
| 124 | + <code className="p-4 rounded bg-gray-200 text-gray-800 font-mono text-xs"> |
| 125 | + {embedCode} |
| 126 | + </code> |
| 127 | + <CopyButton text={embedCode} /> |
| 128 | + </div> |
| 129 | + </label> |
| 130 | + </section> |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +export default Embed; |
0 commit comments