@redvars/peacock 3.16.0 → 3.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/dist/assets/styles.css +1 -1
  2. package/dist/assets/styles.css.map +1 -1
  3. package/dist/assets/theme.js +19 -19
  4. package/dist/assets/tokens.css +1 -1
  5. package/dist/assets/tokens.css.map +1 -1
  6. package/dist/card-content.js +8 -7
  7. package/dist/card-content.js.map +1 -1
  8. package/dist/card.js +142 -98
  9. package/dist/card.js.map +1 -1
  10. package/dist/{chart-bar-D5dGFJfK.js → cartesian-chart-DgE4USyC.js} +529 -312
  11. package/dist/cartesian-chart-DgE4USyC.js.map +1 -0
  12. package/dist/chart-area.js +78 -0
  13. package/dist/chart-area.js.map +1 -0
  14. package/dist/chart-bar.js +283 -213
  15. package/dist/chart-bar.js.map +1 -1
  16. package/dist/{transform-DSwFSqzD.js → chart-base-C2qRMF48.js} +771 -102
  17. package/dist/chart-base-C2qRMF48.js.map +1 -0
  18. package/dist/chart-doughnut.js +55 -287
  19. package/dist/chart-doughnut.js.map +1 -1
  20. package/dist/chart-line.js +626 -0
  21. package/dist/chart-line.js.map +1 -0
  22. package/dist/chart-pie.js +637 -195
  23. package/dist/chart-pie.js.map +1 -1
  24. package/dist/code-editor.js +704 -131
  25. package/dist/code-editor.js.map +1 -1
  26. package/dist/code-group.js +188 -0
  27. package/dist/code-group.js.map +1 -0
  28. package/dist/code-highlighter.js +895 -257
  29. package/dist/code-highlighter.js.map +1 -1
  30. package/dist/custom-elements-jsdocs.json +1675 -222
  31. package/dist/custom-elements.json +13351 -3970
  32. package/dist/image.js +5 -1
  33. package/dist/image.js.map +1 -1
  34. package/dist/index.js +8 -7
  35. package/dist/index.js.map +1 -1
  36. package/dist/loader-full.js +1 -1
  37. package/dist/loader.js +9 -3
  38. package/dist/loader.js.map +1 -1
  39. package/dist/{is-dark-mode-DOcaw4Yq.js → observe-theme-change-DCifz1uv.js} +2 -6
  40. package/dist/observe-theme-change-DCifz1uv.js.map +1 -0
  41. package/dist/path-CJ0Arqcy.js +166 -0
  42. package/dist/path-CJ0Arqcy.js.map +1 -0
  43. package/dist/snackbar.js +89 -34
  44. package/dist/snackbar.js.map +1 -1
  45. package/dist/src/__internal/chart/cartesian-chart.d.ts +80 -0
  46. package/dist/src/__internal/chart/chart-base.d.ts +113 -0
  47. package/dist/src/__internal/chart/paths.d.ts +5 -0
  48. package/dist/src/__internal/chart/types.d.ts +42 -0
  49. package/dist/src/card/card-content.d.ts +2 -3
  50. package/dist/src/card/card.d.ts +31 -17
  51. package/dist/src/chart-area/chart-area.d.ts +54 -0
  52. package/dist/src/chart-area/index.d.ts +1 -0
  53. package/dist/src/chart-bar/chart-bar.d.ts +44 -33
  54. package/dist/src/chart-bar/index.d.ts +0 -1
  55. package/dist/src/chart-doughnut/chart-doughnut.d.ts +33 -39
  56. package/dist/src/chart-line/chart-line.d.ts +69 -0
  57. package/dist/src/chart-line/index.d.ts +1 -0
  58. package/dist/src/chart-pie/chart-pie.d.ts +63 -28
  59. package/dist/src/code-editor/code-editor.d.ts +52 -13
  60. package/dist/src/code-editor/editor-theme.d.ts +60 -0
  61. package/dist/src/code-highlighter/code-group.d.ts +52 -0
  62. package/dist/src/code-highlighter/code-highlighter.d.ts +122 -15
  63. package/dist/src/code-highlighter/index.d.ts +1 -0
  64. package/dist/src/code-highlighter/line-ranges.d.ts +24 -0
  65. package/dist/src/code-highlighter/local.d.ts +4 -1
  66. package/dist/src/code-highlighter/token-colors.d.ts +8 -0
  67. package/dist/src/index.d.ts +4 -2
  68. package/dist/src/snackbar/snackbar.d.ts +13 -1
  69. package/dist/src/toolbar/toolbar.d.ts +56 -51
  70. package/dist/table.js +9 -4
  71. package/dist/table.js.map +1 -1
  72. package/dist/test/chart.test.d.ts +1 -0
  73. package/dist/test/code-highlighter.test.d.ts +1 -0
  74. package/dist/token-colors-0TFHWbcq.js +81 -0
  75. package/dist/token-colors-0TFHWbcq.js.map +1 -0
  76. package/dist/toolbar.js +195 -184
  77. package/dist/toolbar.js.map +1 -1
  78. package/dist/tsconfig.tsbuildinfo +1 -1
  79. package/package.json +1 -2
  80. package/readme.md +7 -6
  81. package/scss/_code-block.scss +139 -0
  82. package/scss/tokens.css +24 -0
  83. package/src/__internal/chart/cartesian-chart.ts +404 -0
  84. package/src/__internal/chart/chart-base.scss +252 -0
  85. package/src/__internal/chart/chart-base.ts +474 -0
  86. package/src/__internal/chart/paths.ts +28 -0
  87. package/src/__internal/chart/types.ts +49 -0
  88. package/src/card/card-content.ts +12 -9
  89. package/src/card/card.scss +66 -54
  90. package/src/card/card.ts +109 -73
  91. package/src/chart-area/chart-area.ts +82 -0
  92. package/src/chart-area/index.ts +1 -0
  93. package/src/chart-bar/chart-bar.ts +336 -250
  94. package/src/chart-bar/index.ts +0 -1
  95. package/src/chart-doughnut/chart-doughnut.ts +62 -262
  96. package/src/chart-doughnut/demo/index.html +14 -22
  97. package/src/chart-line/chart-line.scss +25 -0
  98. package/src/chart-line/chart-line.ts +346 -0
  99. package/src/chart-line/index.ts +1 -0
  100. package/src/chart-pie/chart-pie.scss +35 -16
  101. package/src/chart-pie/chart-pie.ts +282 -188
  102. package/src/chart-pie/demo/index.html +13 -21
  103. package/src/code-editor/code-editor.scss +132 -36
  104. package/src/code-editor/code-editor.ts +308 -88
  105. package/src/code-editor/demo/index.html +39 -12
  106. package/src/code-editor/editor-theme.ts +213 -0
  107. package/src/code-highlighter/code-group.scss +34 -0
  108. package/src/code-highlighter/code-group.ts +173 -0
  109. package/src/code-highlighter/code-highlighter.scss +226 -111
  110. package/src/code-highlighter/code-highlighter.ts +554 -199
  111. package/src/code-highlighter/demo/index.html +68 -4
  112. package/src/code-highlighter/index.ts +1 -0
  113. package/src/code-highlighter/line-ranges.ts +81 -0
  114. package/src/code-highlighter/local.ts +9 -3
  115. package/src/code-highlighter/token-colors.ts +42 -0
  116. package/src/index.ts +4 -2
  117. package/src/loader.ts +8 -2
  118. package/src/snackbar/demo/index.html +39 -4
  119. package/src/snackbar/snackbar.scss +27 -10
  120. package/src/snackbar/snackbar.ts +80 -37
  121. package/src/table/table.scss +7 -1
  122. package/src/table/table.ts +4 -3
  123. package/src/toolbar/toolbar-colors.scss +6 -7
  124. package/src/toolbar/toolbar.scss +35 -106
  125. package/src/toolbar/toolbar.ts +180 -80
  126. package/dist/array-D5vjT2Xm.js +0 -14
  127. package/dist/array-D5vjT2Xm.js.map +0 -1
  128. package/dist/chart-bar-D5dGFJfK.js.map +0 -1
  129. package/dist/chart-stacked-bar.js +0 -403
  130. package/dist/chart-stacked-bar.js.map +0 -1
  131. package/dist/copy-to-clipboard-upGx-Ueo.js +0 -39
  132. package/dist/copy-to-clipboard-upGx-Ueo.js.map +0 -1
  133. package/dist/is-dark-mode-DOcaw4Yq.js.map +0 -1
  134. package/dist/pie-Dz0IDiPt.js +0 -537
  135. package/dist/pie-Dz0IDiPt.js.map +0 -1
  136. package/dist/pierre-dark-DFWl0m-C.js +0 -4
  137. package/dist/pierre-dark-DFWl0m-C.js.map +0 -1
  138. package/dist/pierre-light-BEkAPImt.js +0 -4
  139. package/dist/pierre-light-BEkAPImt.js.map +0 -1
  140. package/dist/src/chart-bar/chart-stacked-bar.d.ts +0 -78
  141. package/dist/src/code-highlighter/constants.d.ts +0 -1
  142. package/dist/transform-DSwFSqzD.js.map +0 -1
  143. package/src/chart-bar/chart-bar.scss +0 -58
  144. package/src/chart-bar/chart-stacked-bar.ts +0 -398
  145. package/src/chart-doughnut/chart-donut.scss +0 -37
  146. package/src/code-highlighter/constants.ts +0 -117
  147. /package/dist/test/{chart-bar.test.d.ts → card.test.d.ts} +0 -0
@@ -1,5 +1,5 @@
1
1
  <!doctype html>
2
- <html lang="en-GB" data-theme="dark">
2
+ <html lang="en-GB">
3
3
  <head>
4
4
  <meta charset="utf-8" />
5
5
  <meta
@@ -14,29 +14,56 @@
14
14
 
15
15
  <style>
16
16
  body {
17
+ display: grid;
18
+ gap: 1.5rem;
19
+ max-width: 48rem;
20
+ margin: 2rem auto;
21
+ padding: 0 1rem;
17
22
  background: var(--color-surface);
23
+ color: var(--color-on-surface);
18
24
  }
19
25
  </style>
20
26
  </head>
21
27
  <body>
22
- <code-editor
23
- style="height: 400px"
28
+ <button
29
+ type="button"
30
+ onclick="document.documentElement.dataset.theme = document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark'"
31
+ >
32
+ Toggle dark theme
33
+ </button>
34
+
35
+ <wc-code-editor
24
36
  id="business-rule-editor"
37
+ label="business-rule.js"
25
38
  language="javascript"
26
- ></code-editor>
39
+ wrap-toggle
40
+ >
41
+ <wc-icon slot="icon" name="description"></wc-icon>
42
+ </wc-code-editor>
43
+
44
+ <wc-code-editor
45
+ id="read-only-editor"
46
+ language="css"
47
+ readonly
48
+ show-readonly-badge
49
+ style="--code-editor-height: 8rem"
50
+ ></wc-code-editor>
27
51
 
28
52
  <script type="module">
29
53
  import '/dist/loader.js';
30
54
 
31
55
  document.getElementById('business-rule-editor').value = `/**
32
- * Sample Code
33
- */
34
- function myBusinessRuleFunction(){
35
- const a = 1;
36
- const b = 2;
37
- let c = a + b;
38
- console.log(c);
39
- }`;
56
+ * Sample Code
57
+ */
58
+ function myBusinessRuleFunction() {
59
+ const a = 1;
60
+ const b = 2;
61
+ let c = a + b;
62
+ console.log(c);
63
+ }`;
64
+ document.getElementById('read-only-editor').value = `:root {
65
+ --color-primary: var(--color-primary-40);
66
+ }`;
40
67
  </script>
41
68
  </body>
42
69
  </html>
@@ -0,0 +1,213 @@
1
+ import { tokenColor } from '../code-highlighter/token-colors.js';
2
+
3
+ /**
4
+ * The colors the editor theme is built from: Peacock color roles, plus the syntax colors
5
+ * wc-code-highlighter uses, so the two show code alike. Monaco takes hex colors only, so these
6
+ * are resolved on the page and converted.
7
+ */
8
+ export const PALETTE_SOURCES = {
9
+ surface: 'var(--color-surface)',
10
+ surfaceContainerHigh: 'var(--color-surface-container-high)',
11
+ surfaceContainerHighest: 'var(--color-surface-container-highest)',
12
+ onSurface: 'var(--color-on-surface)',
13
+ onSurfaceVariant: 'var(--color-on-surface-variant)',
14
+ outline: 'var(--color-outline)',
15
+ outlineVariant: 'var(--color-outline-variant)',
16
+ primary: 'var(--color-primary)',
17
+ secondary: 'var(--color-secondary)',
18
+ tertiary: 'var(--color-tertiary)',
19
+ secondaryContainer: 'var(--color-secondary-container)',
20
+ onSecondaryContainer: 'var(--color-on-secondary-container)',
21
+ error: 'var(--color-error)',
22
+ warning: 'var(--color-warning)',
23
+ info: 'var(--color-blue)',
24
+ foreground: tokenColor('foreground'),
25
+ keyword: tokenColor('token-keyword'),
26
+ string: tokenColor('token-string'),
27
+ func: tokenColor('token-function'),
28
+ constant: tokenColor('token-constant'),
29
+ comment: tokenColor('token-comment'),
30
+ punctuation: tokenColor('token-punctuation'),
31
+ parameter: tokenColor('token-parameter'),
32
+ link: tokenColor('token-link'),
33
+ } as const;
34
+
35
+ export type EditorPalette = Record<keyof typeof PALETTE_SOURCES, string>;
36
+
37
+ /** Monaco's standalone theme data, as `monaco.editor.defineTheme` takes it. */
38
+ export type EditorThemeData = {
39
+ base: 'vs' | 'vs-dark';
40
+ inherit: boolean;
41
+ rules: { token: string; foreground?: string; fontStyle?: string }[];
42
+ colors: Record<string, string>;
43
+ };
44
+
45
+ let context: CanvasRenderingContext2D | null | undefined;
46
+
47
+ /**
48
+ * Converts any CSS color the browser understands (`rgb()`, `oklch()`, `color()`, …) to
49
+ * `#rrggbb`, or `#rrggbbaa` when it is not opaque, by painting one pixel.
50
+ */
51
+ export function cssColorToHex(color: string): string {
52
+ context ??= document
53
+ .createElement('canvas')
54
+ .getContext('2d', { willReadFrequently: true });
55
+ if (!context) return '#000000';
56
+ context.clearRect(0, 0, 1, 1);
57
+ context.fillStyle = '#000000';
58
+ context.fillStyle = color;
59
+ context.fillRect(0, 0, 1, 1);
60
+ const [r, g, b, a] = context.getImageData(0, 0, 1, 1).data;
61
+ const hex = [r, g, b, ...(a < 255 ? [a] : [])]
62
+ .map(value => value.toString(16).padStart(2, '0'))
63
+ .join('');
64
+ return `#${hex}`;
65
+ }
66
+
67
+ /**
68
+ * Resolves the palette where `probe` sits, so it follows the theme around the editor:
69
+ * `data-theme`, the system color scheme, or a custom theme.
70
+ */
71
+ export function resolvePalette(probe: HTMLElement): EditorPalette {
72
+ const palette = {} as EditorPalette;
73
+ for (const [key, value] of Object.entries(PALETTE_SOURCES)) {
74
+ // eslint-disable-next-line no-param-reassign
75
+ probe.style.color = value;
76
+ palette[key as keyof EditorPalette] = cssColorToHex(
77
+ getComputedStyle(probe).color,
78
+ );
79
+ }
80
+ // eslint-disable-next-line no-param-reassign
81
+ probe.style.color = '';
82
+ return palette;
83
+ }
84
+
85
+ /** `#rrggbb` with an alpha from 0 to 1. */
86
+ function alpha(hex: string, opacity: number): string {
87
+ const value = Math.round(Math.min(1, Math.max(0, opacity)) * 255);
88
+ return `${hex.slice(0, 7)}${value.toString(16).padStart(2, '0')}`;
89
+ }
90
+
91
+ /** Whether a `#rrggbb` color is dark, by relative luminance. */
92
+ export function isDark(hex: string): boolean {
93
+ const [r, g, b] = [1, 3, 5].map(index => {
94
+ const channel = parseInt(hex.slice(index, index + 2), 16) / 255;
95
+ return channel <= 0.04045
96
+ ? channel / 12.92
97
+ : ((channel + 0.055) / 1.055) ** 2.4;
98
+ });
99
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b < 0.18;
100
+ }
101
+
102
+ /** Monaco rules take colors without the `#`. */
103
+ const bare = (hex: string) => hex.slice(1, 7);
104
+
105
+ /**
106
+ * Builds the editor theme from a palette. The editor background is transparent, so the frame
107
+ * around it shows through; widgets such as suggestions and hovers sit on surface containers.
108
+ */
109
+ export function buildEditorTheme(p: EditorPalette): EditorThemeData {
110
+ return {
111
+ base: isDark(p.surface) ? 'vs-dark' : 'vs',
112
+ // Not inherited: Monaco's own rules are more specific than these (`string.value.json`,
113
+ // `keyword.flow`, …) and would keep its stock colors.
114
+ inherit: false,
115
+ rules: [
116
+ { token: '', foreground: bare(p.foreground) },
117
+ { token: 'comment', foreground: bare(p.comment), fontStyle: 'italic' },
118
+ { token: 'keyword', foreground: bare(p.keyword) },
119
+ { token: 'string', foreground: bare(p.string) },
120
+ { token: 'string.escape', foreground: bare(p.constant) },
121
+ { token: 'string.key.json', foreground: bare(p.keyword) },
122
+ { token: 'string.link', foreground: bare(p.link) },
123
+ { token: 'number', foreground: bare(p.constant) },
124
+ { token: 'constant', foreground: bare(p.constant) },
125
+ { token: 'regexp', foreground: bare(p.string) },
126
+ { token: 'type', foreground: bare(p.func) },
127
+ { token: 'identifier', foreground: bare(p.foreground) },
128
+ { token: 'variable', foreground: bare(p.parameter) },
129
+ { token: 'predefined', foreground: bare(p.func) },
130
+ { token: 'annotation', foreground: bare(p.func) },
131
+ { token: 'delimiter', foreground: bare(p.punctuation) },
132
+ { token: 'operator', foreground: bare(p.punctuation) },
133
+ { token: 'tag', foreground: bare(p.string) },
134
+ { token: 'metatag', foreground: bare(p.keyword) },
135
+ { token: 'attribute.name', foreground: bare(p.func) },
136
+ { token: 'attribute.value', foreground: bare(p.string) },
137
+ { token: 'tag.css', foreground: bare(p.func) },
138
+ { token: 'attribute.name.css', foreground: bare(p.constant) },
139
+ { token: 'attribute.value.css', foreground: bare(p.foreground) },
140
+ { token: 'attribute.value.number.css', foreground: bare(p.constant) },
141
+ { token: 'attribute.value.unit.css', foreground: bare(p.constant) },
142
+ { token: 'attribute.value.hex.css', foreground: bare(p.constant) },
143
+ { token: 'strong', fontStyle: 'bold' },
144
+ { token: 'emphasis', fontStyle: 'italic' },
145
+ { token: 'keyword.json', foreground: bare(p.constant) },
146
+ { token: 'invalid', foreground: bare(p.error) },
147
+ ],
148
+ colors: {
149
+ foreground: p.onSurface,
150
+ focusBorder: p.primary,
151
+ 'editor.background': '#00000000',
152
+ 'editor.foreground': p.foreground,
153
+ 'editorGutter.background': '#00000000',
154
+ 'editorLineNumber.foreground': p.onSurfaceVariant,
155
+ 'editorLineNumber.activeForeground': p.onSurface,
156
+ 'editor.lineHighlightBackground': alpha(p.onSurface, 0.05),
157
+ 'editor.lineHighlightBorder': '#00000000',
158
+ 'editorCursor.foreground': p.primary,
159
+ 'editor.selectionBackground': alpha(p.primary, 0.24),
160
+ 'editor.inactiveSelectionBackground': alpha(p.primary, 0.12),
161
+ 'editor.selectionHighlightBackground': alpha(p.primary, 0.12),
162
+ 'editor.wordHighlightBackground': alpha(p.primary, 0.1),
163
+ 'editor.wordHighlightStrongBackground': alpha(p.primary, 0.16),
164
+ 'editor.findMatchBackground': alpha(p.warning, 0.4),
165
+ 'editor.findMatchHighlightBackground': alpha(p.warning, 0.2),
166
+ 'editorBracketMatch.background': alpha(p.primary, 0.12),
167
+ 'editorBracketMatch.border': alpha(p.primary, 0.5),
168
+ 'editorIndentGuide.background1': alpha(p.outlineVariant, 0.6),
169
+ 'editorIndentGuide.activeBackground1': p.outline,
170
+ 'editorWhitespace.foreground': p.outlineVariant,
171
+ 'editorError.foreground': p.error,
172
+ 'editorWarning.foreground': p.warning,
173
+ 'editorInfo.foreground': p.info,
174
+ 'editorOverviewRuler.border': '#00000000',
175
+ 'editorOverviewRuler.errorForeground': p.error,
176
+ 'editorOverviewRuler.warningForeground': p.warning,
177
+ 'editorWidget.background': p.surfaceContainerHigh,
178
+ 'editorWidget.foreground': p.onSurface,
179
+ 'editorWidget.border': p.outlineVariant,
180
+ 'editorSuggestWidget.background': p.surfaceContainerHigh,
181
+ 'editorSuggestWidget.border': p.outlineVariant,
182
+ 'editorSuggestWidget.foreground': p.onSurface,
183
+ 'editorSuggestWidget.highlightForeground': p.primary,
184
+ 'editorSuggestWidget.selectedBackground': p.secondaryContainer,
185
+ 'editorSuggestWidget.selectedForeground': p.onSecondaryContainer,
186
+ 'editorHoverWidget.background': p.surfaceContainerHigh,
187
+ 'editorHoverWidget.foreground': p.onSurface,
188
+ 'editorHoverWidget.border': p.outlineVariant,
189
+ 'input.background': p.surfaceContainerHighest,
190
+ 'input.foreground': p.onSurface,
191
+ 'input.border': p.outlineVariant,
192
+ 'inputOption.activeBorder': p.primary,
193
+ 'list.hoverBackground': alpha(p.onSurface, 0.08),
194
+ 'list.activeSelectionBackground': p.secondaryContainer,
195
+ 'list.activeSelectionForeground': p.onSecondaryContainer,
196
+ 'list.highlightForeground': p.primary,
197
+ 'scrollbar.shadow': '#00000000',
198
+ 'scrollbarSlider.background': alpha(p.onSurface, 0.16),
199
+ 'scrollbarSlider.hoverBackground': alpha(p.onSurface, 0.28),
200
+ 'scrollbarSlider.activeBackground': alpha(p.onSurface, 0.38),
201
+ 'minimap.background': '#00000000',
202
+ 'widget.shadow': alpha('#000000', 0.2),
203
+ // Bracket pairs cycle through the theme's accent roles.
204
+ 'editorBracketHighlight.foreground1': p.primary,
205
+ 'editorBracketHighlight.foreground2': p.tertiary,
206
+ 'editorBracketHighlight.foreground3': p.secondary,
207
+ 'editorBracketHighlight.foreground4': p.primary,
208
+ 'editorBracketHighlight.foreground5': p.tertiary,
209
+ 'editorBracketHighlight.foreground6': p.secondary,
210
+ 'editorBracketHighlight.unexpectedBracket.foreground': p.error,
211
+ },
212
+ };
213
+ }
@@ -0,0 +1,34 @@
1
+ @use '../../scss/mixin';
2
+
3
+ @include mixin.base-styles;
4
+
5
+ :host {
6
+ display: block;
7
+ min-width: 0;
8
+ }
9
+
10
+ // The group draws the frame the blocks would otherwise draw, with the same
11
+ // public custom properties, so a themed block and a themed group match.
12
+ .code-group {
13
+ display: flex;
14
+ flex-direction: column;
15
+ min-width: 0;
16
+ overflow: hidden;
17
+ background: var(
18
+ --code-highlighter-background,
19
+ var(--color-surface-container-low)
20
+ );
21
+ border: 1px solid
22
+ var(--code-highlighter-border-color, var(--color-outline-variant));
23
+ border-radius: var(--code-highlighter-shape, var(--shape-corner-medium));
24
+ }
25
+
26
+ .header {
27
+ min-width: 0;
28
+ overflow-x: auto;
29
+ scrollbar-width: none;
30
+ }
31
+
32
+ ::slotted([hidden]) {
33
+ display: none !important;
34
+ }
@@ -0,0 +1,173 @@
1
+ import { html, LitElement } from 'lit';
2
+ import { property, state } from 'lit/decorators.js';
3
+ import IndividualComponent from '@/IndividualComponent.js';
4
+ import styles from './code-group.scss';
5
+
6
+ /** Minimal shape of a grouped `wc-code-highlighter`. */
7
+ type CodeBlockElement = HTMLElement & { label?: string; language?: string };
8
+
9
+ /**
10
+ * @label Code Group
11
+ * @tag wc-code-group
12
+ * @rawTag code-group
13
+ *
14
+ * @summary Shows several code blocks in one frame, with a tab for each block.
15
+ * @overview
16
+ * <p>Put <code>wc-code-highlighter</code> elements inside. Each block's <code>label</code> names its tab,
17
+ * and only the selected block is shown. The group draws the frame, so the blocks drop their own
18
+ * border and header, and their actions float at the top end.</p>
19
+ *
20
+ * @example
21
+ * ```html
22
+ * <wc-code-group>
23
+ * <wc-code-highlighter label="greeting.ts" language="typescript"><pre><code>
24
+ * export const greeting = 'Hello';</code></pre></wc-code-highlighter>
25
+ * <wc-code-highlighter label="greeting.css" language="css"><pre><code>
26
+ * p { color: var(--color-primary); }</code></pre></wc-code-highlighter>
27
+ * </wc-code-group>
28
+ * ```
29
+ *
30
+ * @slot - `wc-code-highlighter` elements, one per tab.
31
+ *
32
+ * @csspart container - The outer frame.
33
+ * @csspart header - The tab bar.
34
+ *
35
+ * @fires {CustomEvent<{index: number}>} change - Dispatched when the user selects another tab.
36
+ *
37
+ * @tags display
38
+ */
39
+ @IndividualComponent
40
+ export class CodeGroup extends LitElement {
41
+ // ── Static ────────────────────────────────────────────────────────────────
42
+
43
+ static styles = [styles];
44
+
45
+ // ── Properties ────────────────────────────────────────────────────────────
46
+
47
+ /** Index of the shown block. */
48
+ @property({ attribute: 'selected-index', type: Number, reflect: true })
49
+ selectedIndex: number = 0;
50
+
51
+ /** The grouped code blocks, in slot order. */
52
+ @state() private _blocks: CodeBlockElement[] = [];
53
+
54
+ // ── Private fields ────────────────────────────────────────────────────────
55
+
56
+ /** Re-renders the tabs when a block's label changes. */
57
+ private __labelObserver?: MutationObserver;
58
+
59
+ // ── Lifecycle ─────────────────────────────────────────────────────────────
60
+
61
+ connectedCallback() {
62
+ super.connectedCallback();
63
+ this.__labelObserver = new MutationObserver(() => this.requestUpdate());
64
+ this.__labelObserver.observe(this, {
65
+ subtree: true,
66
+ attributes: true,
67
+ attributeFilter: ['label', 'language'],
68
+ });
69
+ }
70
+
71
+ disconnectedCallback() {
72
+ this.__labelObserver?.disconnect();
73
+ super.disconnectedCallback();
74
+ }
75
+
76
+ protected updated(changed: Map<PropertyKey, unknown>) {
77
+ if (changed.has('selectedIndex') || changed.has('_blocks')) {
78
+ this.__syncBlocks();
79
+ }
80
+ }
81
+
82
+ // ── Private methods ───────────────────────────────────────────────────────
83
+
84
+ /** The selected index, kept within the blocks. */
85
+ private get __activeIndex(): number {
86
+ const last = this._blocks.length - 1;
87
+ return Math.min(Math.max(0, this.selectedIndex || 0), Math.max(0, last));
88
+ }
89
+
90
+ /** Marks every block as grouped and shows only the selected one. */
91
+ private __syncBlocks() {
92
+ const active = this.__activeIndex;
93
+ this._blocks.forEach((block, index) => {
94
+ block.setAttribute('grouped', '');
95
+ block.toggleAttribute('hidden', index !== active);
96
+ });
97
+ }
98
+
99
+ private __handleSlotChange(event: Event) {
100
+ const slot = event.target as HTMLSlotElement;
101
+ const blocks = slot
102
+ .assignedElements({ flatten: true })
103
+ .filter(
104
+ element => element.localName === 'wc-code-highlighter',
105
+ ) as CodeBlockElement[];
106
+
107
+ // Blocks moved out of the group get their own frame back.
108
+ for (const block of this._blocks) {
109
+ if (!blocks.includes(block)) {
110
+ block.removeAttribute('grouped');
111
+ block.removeAttribute('hidden');
112
+ }
113
+ }
114
+ this._blocks = blocks;
115
+ }
116
+
117
+ private __handleTabChange(event: CustomEvent<{ index: number }>) {
118
+ event.stopPropagation();
119
+ const { index } = event.detail;
120
+ if (index < 0 || index === this.__activeIndex) return;
121
+ this.selectedIndex = index;
122
+ this.dispatchEvent(
123
+ new CustomEvent('change', {
124
+ detail: { index },
125
+ bubbles: true,
126
+ composed: true,
127
+ }),
128
+ );
129
+ }
130
+
131
+ // eslint-disable-next-line class-methods-use-this
132
+ private __stopEvent(event: Event) {
133
+ event.stopPropagation();
134
+ }
135
+
136
+ // ── Render helpers ────────────────────────────────────────────────────────
137
+
138
+ // eslint-disable-next-line class-methods-use-this
139
+ private __tabLabel(block: CodeBlockElement, index: number): string {
140
+ return (
141
+ block.label ||
142
+ block.getAttribute('label') ||
143
+ block.language ||
144
+ `${index + 1}`
145
+ );
146
+ }
147
+
148
+ // ── Render ────────────────────────────────────────────────────────────────
149
+
150
+ render() {
151
+ const active = this.__activeIndex;
152
+ return html`
153
+ <div class="code-group" part="container">
154
+ <div class="header" part="header">
155
+ <wc-tabs
156
+ class="tabs"
157
+ variant="secondary"
158
+ @change=${this.__handleTabChange}
159
+ @tab-click=${this.__stopEvent}
160
+ >
161
+ ${this._blocks.map(
162
+ (block, index) =>
163
+ html`<wc-tab .active=${index === active}
164
+ >${this.__tabLabel(block, index)}</wc-tab
165
+ >`,
166
+ )}
167
+ </wc-tabs>
168
+ </div>
169
+ <slot @slotchange=${this.__handleSlotChange}></slot>
170
+ </div>
171
+ `;
172
+ }
173
+ }