|
| 1 | +import { Box, Text } from '@devup-ui/react' |
| 2 | + |
| 3 | +import { CodeWindow, HighlightedCode } from './code-window' |
| 4 | + |
| 5 | +export interface CodeExample { |
| 6 | + key: string |
| 7 | + label: string |
| 8 | + file: string |
| 9 | + html: string |
| 10 | +} |
| 11 | + |
| 12 | +export type CodeExampleQuad = [CodeExample, CodeExample, CodeExample, CodeExample] |
| 13 | + |
| 14 | +// devup-ui extracts `selectors` strings at build time, so each of the 4 |
| 15 | +// fixed tabs is written out literally rather than generated by a .map() |
| 16 | +// over a runtime key — a template-literal selector key can't be statically |
| 17 | +// extracted into CSS. |
| 18 | +// |
| 19 | +// Each target element (label / title / panel) declares its own "when am I |
| 20 | +// shown" condition anchored on the checkbox's id (`#code-tab-N:checked ~ * |
| 21 | +// &`), rather than the checkbox declaring rules for elements elsewhere in |
| 22 | +// the tree. The checkbox's own `:checked` state isn't observable from the |
| 23 | +// checkbox's position in isolation, so ownership of the visibility rule |
| 24 | +// belongs on the element it affects. |
| 25 | +export function CodeTabs({ examples: [a, b, c, d] }: { examples: CodeExampleQuad }) { |
| 26 | + return ( |
| 27 | + <Box> |
| 28 | + <Box |
| 29 | + as="input" |
| 30 | + boxSize="1px" |
| 31 | + defaultChecked |
| 32 | + id="code-tab-0" |
| 33 | + name="code-tab" |
| 34 | + opacity="0" |
| 35 | + overflow="hidden" |
| 36 | + position="absolute" |
| 37 | + type="radio" |
| 38 | + /> |
| 39 | + <Box |
| 40 | + as="input" |
| 41 | + boxSize="1px" |
| 42 | + id="code-tab-1" |
| 43 | + name="code-tab" |
| 44 | + opacity="0" |
| 45 | + overflow="hidden" |
| 46 | + position="absolute" |
| 47 | + type="radio" |
| 48 | + /> |
| 49 | + <Box |
| 50 | + as="input" |
| 51 | + boxSize="1px" |
| 52 | + id="code-tab-2" |
| 53 | + name="code-tab" |
| 54 | + opacity="0" |
| 55 | + overflow="hidden" |
| 56 | + position="absolute" |
| 57 | + type="radio" |
| 58 | + /> |
| 59 | + <Box |
| 60 | + as="input" |
| 61 | + boxSize="1px" |
| 62 | + id="code-tab-3" |
| 63 | + name="code-tab" |
| 64 | + opacity="0" |
| 65 | + overflow="hidden" |
| 66 | + position="absolute" |
| 67 | + type="radio" |
| 68 | + /> |
| 69 | + <CodeWindow |
| 70 | + tabs={ |
| 71 | + <> |
| 72 | + <Box |
| 73 | + as="label" |
| 74 | + border="1px solid transparent" |
| 75 | + borderRadius="4px" |
| 76 | + color="$caption" |
| 77 | + cursor="pointer" |
| 78 | + htmlFor="code-tab-0" |
| 79 | + px="12px" |
| 80 | + py="6px" |
| 81 | + selectors={{ |
| 82 | + '#code-tab-0:checked ~ * &': { |
| 83 | + bg: '$containerBackground', |
| 84 | + border: '1px solid $border', |
| 85 | + color: '$title', |
| 86 | + }, |
| 87 | + '#code-tab-0:focus-visible ~ * &': { |
| 88 | + boxShadow: '0 0 0 2px $vespertidePrimary', |
| 89 | + }, |
| 90 | + }} |
| 91 | + transition="color .15s, background .15s" |
| 92 | + typography="eyebrow" |
| 93 | + > |
| 94 | + {a.label} |
| 95 | + </Box> |
| 96 | + <Box |
| 97 | + as="label" |
| 98 | + border="1px solid transparent" |
| 99 | + borderRadius="4px" |
| 100 | + color="$caption" |
| 101 | + cursor="pointer" |
| 102 | + htmlFor="code-tab-1" |
| 103 | + px="12px" |
| 104 | + py="6px" |
| 105 | + selectors={{ |
| 106 | + '#code-tab-1:checked ~ * &': { |
| 107 | + bg: '$containerBackground', |
| 108 | + border: '1px solid $border', |
| 109 | + color: '$title', |
| 110 | + }, |
| 111 | + '#code-tab-1:focus-visible ~ * &': { |
| 112 | + boxShadow: '0 0 0 2px $vespertidePrimary', |
| 113 | + }, |
| 114 | + }} |
| 115 | + transition="color .15s, background .15s" |
| 116 | + typography="eyebrow" |
| 117 | + > |
| 118 | + {b.label} |
| 119 | + </Box> |
| 120 | + <Box |
| 121 | + as="label" |
| 122 | + border="1px solid transparent" |
| 123 | + borderRadius="4px" |
| 124 | + color="$caption" |
| 125 | + cursor="pointer" |
| 126 | + htmlFor="code-tab-2" |
| 127 | + px="12px" |
| 128 | + py="6px" |
| 129 | + selectors={{ |
| 130 | + '#code-tab-2:checked ~ * &': { |
| 131 | + bg: '$containerBackground', |
| 132 | + border: '1px solid $border', |
| 133 | + color: '$title', |
| 134 | + }, |
| 135 | + '#code-tab-2:focus-visible ~ * &': { |
| 136 | + boxShadow: '0 0 0 2px $vespertidePrimary', |
| 137 | + }, |
| 138 | + }} |
| 139 | + transition="color .15s, background .15s" |
| 140 | + typography="eyebrow" |
| 141 | + > |
| 142 | + {c.label} |
| 143 | + </Box> |
| 144 | + <Box |
| 145 | + as="label" |
| 146 | + border="1px solid transparent" |
| 147 | + borderRadius="4px" |
| 148 | + color="$caption" |
| 149 | + cursor="pointer" |
| 150 | + htmlFor="code-tab-3" |
| 151 | + px="12px" |
| 152 | + py="6px" |
| 153 | + selectors={{ |
| 154 | + '#code-tab-3:checked ~ * &': { |
| 155 | + bg: '$containerBackground', |
| 156 | + border: '1px solid $border', |
| 157 | + color: '$title', |
| 158 | + }, |
| 159 | + '#code-tab-3:focus-visible ~ * &': { |
| 160 | + boxShadow: '0 0 0 2px $vespertidePrimary', |
| 161 | + }, |
| 162 | + }} |
| 163 | + transition="color .15s, background .15s" |
| 164 | + typography="eyebrow" |
| 165 | + > |
| 166 | + {d.label} |
| 167 | + </Box> |
| 168 | + </> |
| 169 | + } |
| 170 | + title={ |
| 171 | + <> |
| 172 | + <Text |
| 173 | + as="span" |
| 174 | + display="none" |
| 175 | + id="code-title-0" |
| 176 | + selectors={{ '#code-tab-0:checked ~ * &': { display: 'block' } }} |
| 177 | + > |
| 178 | + {a.file} |
| 179 | + </Text> |
| 180 | + <Text |
| 181 | + as="span" |
| 182 | + display="none" |
| 183 | + id="code-title-1" |
| 184 | + selectors={{ '#code-tab-1:checked ~ * &': { display: 'block' } }} |
| 185 | + > |
| 186 | + {b.file} |
| 187 | + </Text> |
| 188 | + <Text |
| 189 | + as="span" |
| 190 | + display="none" |
| 191 | + id="code-title-2" |
| 192 | + selectors={{ '#code-tab-2:checked ~ * &': { display: 'block' } }} |
| 193 | + > |
| 194 | + {c.file} |
| 195 | + </Text> |
| 196 | + <Text |
| 197 | + as="span" |
| 198 | + display="none" |
| 199 | + id="code-title-3" |
| 200 | + selectors={{ '#code-tab-3:checked ~ * &': { display: 'block' } }} |
| 201 | + > |
| 202 | + {d.file} |
| 203 | + </Text> |
| 204 | + </> |
| 205 | + } |
| 206 | + > |
| 207 | + <Box |
| 208 | + display="none" |
| 209 | + id="code-panel-0" |
| 210 | + selectors={{ '#code-tab-0:checked ~ * &': { display: 'block' } }} |
| 211 | + > |
| 212 | + <HighlightedCode html={a.html} /> |
| 213 | + </Box> |
| 214 | + <Box |
| 215 | + display="none" |
| 216 | + id="code-panel-1" |
| 217 | + selectors={{ '#code-tab-1:checked ~ * &': { display: 'block' } }} |
| 218 | + > |
| 219 | + <HighlightedCode html={b.html} /> |
| 220 | + </Box> |
| 221 | + <Box |
| 222 | + display="none" |
| 223 | + id="code-panel-2" |
| 224 | + selectors={{ '#code-tab-2:checked ~ * &': { display: 'block' } }} |
| 225 | + > |
| 226 | + <HighlightedCode html={c.html} /> |
| 227 | + </Box> |
| 228 | + <Box |
| 229 | + display="none" |
| 230 | + id="code-panel-3" |
| 231 | + selectors={{ '#code-tab-3:checked ~ * &': { display: 'block' } }} |
| 232 | + > |
| 233 | + <HighlightedCode html={d.html} /> |
| 234 | + </Box> |
| 235 | + </CodeWindow> |
| 236 | + </Box> |
| 237 | + ) |
| 238 | +} |
0 commit comments