@codecademy/styleguide 79.0.1-alpha.4aeb6d.0 → 79.0.1-alpha.5950e5.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 (33) hide show
  1. package/.storybook/components/Elements/DocsContainer.tsx +0 -4
  2. package/.storybook/components/Elements/Markdown.tsx +1 -0
  3. package/.storybook/preview.ts +0 -14
  4. package/.storybook/theming/GamutThemeProvider.tsx +1 -7
  5. package/CHANGELOG.md +1 -1
  6. package/package.json +2 -2
  7. package/src/lib/Foundations/System/About.mdx +1 -1
  8. package/src/lib/Foundations/System/Props.mdx +230 -0
  9. package/src/lib/Foundations/System/ResponsiveProperties/ResponsiveProperties.mdx +3 -3
  10. package/src/lib/Foundations/System/ResponsiveProperties/ResponsiveProperties.stories.tsx +0 -1
  11. package/src/lib/Foundations/shared/elements.tsx +19 -69
  12. package/src/lib/Meta/About.mdx +1 -3
  13. package/src/lib/Meta/Usage Guide.mdx +1 -6
  14. package/src/lib/Molecules/Tips/InfoTip/InfoTip.mdx +1 -1
  15. package/src/lib/Organisms/BarChart/BarChart.mdx +414 -0
  16. package/src/lib/Organisms/BarChart/BarChart.stories.tsx +378 -0
  17. package/src/static/meta/toolbar.png +0 -0
  18. package/src/lib/Foundations/System/Props/About.mdx +0 -81
  19. package/src/lib/Foundations/System/Props/Background.mdx +0 -30
  20. package/src/lib/Foundations/System/Props/Border.mdx +0 -53
  21. package/src/lib/Foundations/System/Props/Border.stories.tsx +0 -138
  22. package/src/lib/Foundations/System/Props/Color.mdx +0 -42
  23. package/src/lib/Foundations/System/Props/Color.stories.tsx +0 -47
  24. package/src/lib/Foundations/System/Props/Flex.mdx +0 -28
  25. package/src/lib/Foundations/System/Props/Grid.mdx +0 -31
  26. package/src/lib/Foundations/System/Props/Layout.mdx +0 -34
  27. package/src/lib/Foundations/System/Props/List.mdx +0 -38
  28. package/src/lib/Foundations/System/Props/Positioning.mdx +0 -29
  29. package/src/lib/Foundations/System/Props/Shadow.mdx +0 -31
  30. package/src/lib/Foundations/System/Props/Space.mdx +0 -44
  31. package/src/lib/Foundations/System/Props/Space.stories.tsx +0 -48
  32. package/src/lib/Foundations/System/Props/Typography.mdx +0 -28
  33. package/src/lib/Meta/Logical and physical CSS properties.mdx +0 -123
@@ -0,0 +1,414 @@
1
+ import { Canvas, Controls, Meta } from '@storybook/blocks';
2
+
3
+ import {
4
+ Callout,
5
+ ComponentHeader,
6
+ ImageWrapper,
7
+ LinkTo,
8
+ } from '~styleguide/blocks';
9
+
10
+ import * as BarChartStories from './BarChart.stories';
11
+
12
+ export const parameters = {
13
+ title: 'BarChart',
14
+ subtitle: `A horizontal bar chart for visualizing comparative data`,
15
+ design: {
16
+ type: 'figma',
17
+ url: 'https://www.figma.com/design/ReGfRNillGABAj5SlITalN/%F0%9F%93%90-Gamut?node-id=55123-4176',
18
+ },
19
+ status: 'current',
20
+ source: {
21
+ repo: 'gamut',
22
+ githubLink:
23
+ 'https://github.com/Codecademy/gamut/blob/main/packages/gamut/src/BarChart',
24
+ },
25
+ };
26
+
27
+ <Meta of={BarChartStories} />
28
+
29
+ <ComponentHeader {...parameters} />
30
+
31
+ ## Usage
32
+
33
+ Use BarChart to display comparative data across categories, such as skills progress, XP earned, or any quantitative metrics that benefit from visual comparison.
34
+
35
+ ### Best practices:
36
+
37
+ - Use consistent units across all bars in a chart
38
+ - Limit the number of bars to maintain readability (5-10 is optimal)
39
+ - Consider using the stacked variant to show progress toward a goal
40
+ - Sort bars by value (descending) when ranking is important
41
+ - Ensure bar colors maintain at least a 3:1 contrast ratio with the background
42
+
43
+ ### When NOT to use:
44
+
45
+ - **Progress tracking** – for displaying the completion status of a task, use the <LinkTo id="Atoms/ProgressBar">ProgressBar</LinkTo> component.
46
+
47
+ ## Anatomy
48
+
49
+ <ImageWrapper
50
+ src="./organisms/barchart.png"
51
+ alt="The anatomy of the BarChart component, detailed below."
52
+ />
53
+
54
+ 1. **Bar row**
55
+
56
+ - Represents a single category and its values.
57
+
58
+ 2. **Category icon** (optional)
59
+
60
+ - Use to clarify or reinforce the category name.
61
+
62
+ 3. **Category label**
63
+
64
+ - Displays gridlines and labels to indicate the scale of the data values.
65
+ - The number of dividers, as well as the minimum and maximum values on the value axis, are adjustable.
66
+ - The minimum value should always be 0.
67
+
68
+ 4. **Series one value**
69
+
70
+ - Displays the numerical value of the series one bar for clarity and precise data representation.
71
+
72
+ 5. **Series two value** (optional)
73
+
74
+ - Displays the numerical value of the series two bar for clarity and precise data representation.
75
+ - Include a second series value by using the stacked property. Stacked bar charts compare parts of a whole and are best suited for displaying discrete data segmented into meaningful categories.
76
+
77
+ 6. ** Series one bar**
78
+
79
+ - A visual representation of the first value for the associated category.
80
+ - By default, the first series bars use the text color, however, it can be overridden to any color.
81
+ - Bars include a required border that uses white or navy, chosen automatically to ensure the highest contrast with the bar color.
82
+
83
+ 8. **Series two bar** _(bars with zero value)_
84
+
85
+ - A visual representation of the second value for the associated category.
86
+ - By default, the second series bars use the primary color, however, it can be overridden to any color.
87
+ - Bars include a required border that uses white or navy, chosen automatically to ensure the highest contrast with the bar color.
88
+ - Bars include a required border that uses white or navy, chosen automatically to ensure the highest contrast with the bar color.
89
+
90
+ ## Variants
91
+
92
+ ### Simple (Non-stacked)
93
+
94
+ Use the simple variant when showing single values per category. Only `seriesOneValue` is provided.
95
+
96
+ <Canvas of={BarChartStories.Default} />
97
+
98
+ ### Stacked
99
+
100
+ Use the stacked variant when showing progress within a total. Provide both `seriesOneValue` (progress) and `seriesTwoValue` (total).
101
+
102
+ <Canvas of={BarChartStories.Stacked} />
103
+
104
+ ### With Icons
105
+
106
+ Add icons to labels for better visual identification of categories.
107
+
108
+ <Canvas of={BarChartStories.WithIcons} />
109
+
110
+ ### Animated
111
+
112
+ Enable entrance animations for a more engaging experience.
113
+
114
+ <Canvas of={BarChartStories.Animated} />
115
+
116
+ ### Interactive
117
+
118
+ Rows can be made interactive with `onClick` handlers or `href` links.
119
+
120
+ <Canvas of={BarChartStories.Interactive} />
121
+
122
+ ### With Links
123
+
124
+ When a bar has an `href` link, an `aria-label` is automatically generated from the bar's data and applied to the anchor. The label summarizes the bar's values (e.g., "100 XP in Python" for a simple bar, or "200 XP gained - now at 1500 XP in Python" for a stacked bar), so screen reader users get a clear, data-driven description without manual `aria-label` props.
125
+
126
+ <Canvas of={BarChartStories.WithLinks} />
127
+
128
+ ## Title and description
129
+
130
+ The BarChart component uses semantic HTML to provide context and accessibility. The chart is wrapped in a [`<figure>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure) element, and the description is displayed in a [`<figcaption>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figcaption) element. This semantic structure helps screen readers and other assistive technologies understand the relationship between the chart and its description. **Visual title + description is the greatly preferred pattern** for accessibility and user experience.
131
+
132
+ **Preferred pattern: Visual title + description**
133
+
134
+ Provide both `title` and `description` props to make the chart's purpose and key takeaways clear to all users:
135
+
136
+ - `title`: A heading that identifies the chart. Can be a string or an object with `{ as?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6', title: string, variant?: Text['variant'] }` to specify the heading level and optionally override the default styling with a <LinkTo id="Typography/Text">Text variant</LinkTo>.
137
+ - `description`: A summary of the information or the overall takeaway displayed in the `<figcaption>` element.
138
+
139
+ <Canvas of={BarChartStories.WithVisualTitleAndDescription} />
140
+
141
+ **Alternative patterns:**
142
+ `hideTitle` and `hideDescription` will keep the screenreader text but hide the visual descriptions.
143
+
144
+ <Canvas of={BarChartStories.WithHiddenTitleAndDescription} />
145
+
146
+ **External title with visual description:** Use `aria-labelledby` for the title when the title exists elsewhere but you want to display the description.
147
+
148
+ <Canvas of={BarChartStories.WithExternalTitle} />
149
+
150
+ **Best practices:**
151
+
152
+ - Always provide a `description` prop - it's required and should summarize the chart's key information
153
+ - Use semantic heading levels for titles (h1-h6) to maintain proper document structure
154
+ - When using `aria-labelledby`, ensure the referenced element exists and is properly labeled
155
+
156
+ ## Sorting
157
+
158
+ BarChart includes an optional sorting dropdown in the title area. The dropdown only renders when the `sortFns` prop is provided. You can control which sort options appear by including string literals or custom sort functions in the array.
159
+
160
+ **String Literals:**
161
+
162
+ - `'alphabetically'` - Adds both "Label (A-Z)" and "Label (Z-A)" options
163
+ - `'numerically'` - Adds both "Value (Low-High)" and "Value (High-Low)" options
164
+ - `'none'` - Adds "None" option (preserves original order)
165
+
166
+ **Custom Sort Functions:**
167
+ You can also provide custom sort functions as objects with:
168
+
169
+ - `label`: The text displayed in the dropdown
170
+ - `value`: A unique identifier for the sort option
171
+ - `sortFn`: A function that takes an array of bars and returns a sorted array
172
+
173
+ **Type Definition:**
174
+
175
+ ```typescript
176
+ type CustomSortOption<TBar extends BarProps = BarProps> = {
177
+ label: string;
178
+ value: string;
179
+ sortFn: (bars: TBar[]) => TBar[];
180
+ };
181
+
182
+ // sortFns prop accepts:
183
+ sortFns?: ('alphabetically' | 'numerically' | 'none' | CustomSortOption<TBar>)[]
184
+ ```
185
+
186
+ **Automatic Type Inference:**
187
+ TypeScript automatically infers the bar type from your `barValues` prop. This means custom properties (like `dateAdded`, `category`, etc.) are automatically typed in your sort functions - no type assertions needed!
188
+
189
+ ```typescript
190
+ const barsWithDates = [
191
+ { yLabel: 'Python', seriesOneValue: 1500, dateAdded: new Date('2023-01-15') },
192
+ {
193
+ yLabel: 'JavaScript',
194
+ seriesOneValue: 2000,
195
+ dateAdded: new Date('2023-03-20'),
196
+ },
197
+ // ...
198
+ ];
199
+
200
+ <BarChart
201
+ barValues={barsWithDates}
202
+ sortFns={[
203
+ {
204
+ label: 'Recently Added',
205
+ value: 'recent',
206
+ sortFn: (bars) => {
207
+ // TypeScript automatically knows bars have dateAdded property!
208
+ return [...bars].sort((a, b) => {
209
+ const aDate = a.dateAdded as Date | undefined;
210
+ const bDate = b.dateAdded as Date | undefined;
211
+ if (!aDate || !bDate) return 0;
212
+ return bDate.getTime() - aDate.getTime();
213
+ });
214
+ },
215
+ },
216
+ ]}
217
+ />;
218
+ ```
219
+
220
+ **Note:** Since `BarProps` uses `Record<string, unknown>` for extensibility, custom properties are typed as `unknown` by default. You may need a simple type assertion (`as Date | undefined`) when accessing them, but the property names are fully type-checked.
221
+
222
+ <Canvas of={BarChartStories.WithSorting} />
223
+
224
+ **Example with Custom Sort Functions:**
225
+
226
+ Custom sort functions can access additional properties on `BarProps` for domain-specific sorting, such as sorting by dates or categories.
227
+
228
+ <Canvas of={BarChartStories.WithCustomSorting} />
229
+
230
+ ## Custom styling
231
+
232
+ BarChart supports custom color styling through the `styleConfig` prop, allowing you to customize the appearance of chart elements to match your design needs.
233
+
234
+ ### Style configuration options
235
+
236
+ The `styleConfig` prop accepts an object with the following optional properties:
237
+
238
+ - **`textColor`**: Color for text labels (y-axis labels). Defaults to `'text'`
239
+ - **`seriesOneBarColor`**: Color for the series one bar (seriesOneValue: overlay in stacked charts, only bar in simple charts). Defaults to `'text'`
240
+ - **`seriesTwoBarColor`**: Color for the series two bar (seriesTwoValue: full bar in stacked charts; unused in simple charts). Defaults to `'primary'`
241
+ - **`seriesOneLabel`**: Color for the series one value label (first right label when stacked, or displayValue when not stacked). Defaults to `'text-secondary'`
242
+ - **`seriesTwoLabel`**: Color for the series two value label (displayValue when stacked). Defaults to `'primary'`
243
+
244
+ All color values should be valid color tokens from the Gamut design system. When customizing colors, ensure they maintain at least a 3:1 contrast ratio with the background for accessibility.
245
+
246
+ ### Default values
247
+
248
+ If `styleConfig` is not provided, BarChart uses these default colors:
249
+
250
+ ```typescript
251
+ {
252
+ textColor: 'text',
253
+ seriesOneBarColor: 'text',
254
+ seriesTwoBarColor: 'primary',
255
+ seriesOneLabel: 'text-secondary',
256
+ seriesTwoLabel: 'primary',
257
+ }
258
+ ```
259
+
260
+ ### Example
261
+
262
+ <Canvas of={BarChartStories.CustomStyles} />
263
+
264
+ ## Custom scale
265
+
266
+ BarChart allows you to customize the x-axis scale interval using the `xScale` prop, which controls the spacing and granularity of scale markers along the horizontal axis.
267
+
268
+ ### Scale configuration
269
+
270
+ The `xScale` prop determines the interval between scale markers on the x-axis. By default, BarChart automatically calculates an appropriate scale interval based on the `minRange` and `maxRange` values. However, you can override this with a custom interval to achieve more granular or specific scale markings.
271
+
272
+ **When to use custom scale:**
273
+
274
+ - When you need specific scale intervals (e.g., increments of 250, 500, or 1000)
275
+ - When the automatic scale calculation doesn't provide the desired granularity
276
+ - When you want to match scale intervals across multiple charts for consistency
277
+ - When you need scale markers at specific values for better data interpretation
278
+
279
+ **How it works:**
280
+
281
+ The `xScale` value represents the interval between consecutive scale markers. For example:
282
+
283
+ - `xScale: 250` creates markers at 0, 250, 500, 750, 1000, etc.
284
+ - `xScale: 500` creates markers at 0, 500, 1000, 1500, 2000, etc.
285
+
286
+ The number of tick marks is automatically calculated as: `Math.ceil((maxRange - minRange) / xScale) + 1`
287
+
288
+ ### Example
289
+
290
+ <Canvas of={BarChartStories.CustomScale} />
291
+
292
+ ## Color modes
293
+
294
+ BarChart automatically adapts to both light and dark color modes, ensuring proper contrast and readability in all themes. The component uses semantic color tokens that adjust based on the current color mode. The `styleConfig` prop should be provided by semantic color tokens to ensure the `BarChart` is accessible in all color modes.
295
+
296
+ ## Accessibility considerations
297
+
298
+ ### Chart and row labeling
299
+
300
+ **Chart-level labeling:**
301
+
302
+ - Always provide either `title` or `aria-labelledby` to describe the chart (see Title and description section below for details)
303
+ - The chart is wrapped in a semantic `<figure>` element for proper structure
304
+
305
+ **Row-level labeling:**
306
+
307
+ - For non-interactive bars: A hidden text element (visible only to screen readers) is automatically added as the first child of each list item, summarizing the bar's values (e.g., "100 XP in Python")
308
+ - For interactive bars (with `onClick` or `href`): An `aria-label` is automatically generated from the bar's data and applied to the button or link element, ensuring proper screen reader announcements
309
+ - All accessibility labels are automatically generated from the bar's data - no manual `aria-label` props are required
310
+
311
+ ## UX writing
312
+
313
+ - Keep y-axis labels concise (1-3 words)
314
+ - Use consistent unit labels (e.g., "XP", "hours", "points")
315
+ - Consider locale-aware number formatting for international audiences
316
+
317
+ ## Interactive states
318
+
319
+ BarChart supports both interactive and static bar rows, each with distinct visual states.
320
+
321
+ ### Static bars
322
+
323
+ When bars have neither `onClick` handlers nor `href` links, they are rendered as static, non-interactive elements.
324
+
325
+ <Canvas of={BarChartStories.Default} />
326
+
327
+ ### Interactive bars
328
+
329
+ When bars have `onClick` handlers or `href` links, they become interactive. Interactive bars are rendered as buttons or anchor elements, providing full keyboard accessibility and proper semantic HTML.
330
+
331
+ <Canvas of={BarChartStories.Interactive} />
332
+
333
+ ## Internationalization
334
+
335
+ <Callout text="We are currently updating global internationalization in Gamut, so the following features are subject to change." />
336
+
337
+ BarChart supports internationalization through the `translations` prop. You can customize user-facing strings (including sort dropdown and accessibility summaries) and configure locale-aware number formatting. The prop accepts `PartialBarChartTranslations`: override only the keys you need; nested `accessibility` and `sortOptions` are merged with defaults at runtime.
338
+
339
+ ### Translation structure
340
+
341
+ - **sortLabel**, **sortOptions**, and **locale** — Always strings. Control the sort dropdown label, option labels, and number-formatting locale.
342
+ - **Accessibility keys** (`gainedNowAt`, `inLabel`, `inOnly`) — Used in auto-generated `aria-label` and screen reader text for bars. Each key can be:
343
+ - **String** — A fragment interpolated into the built-in template (e.g. `"in"`, `"gained - now at"`). Use when the default word order works for your language.
344
+ - **Function** — Receives scoped context (values, label, unit, locale) and returns the **entire** summary string. Use for pluralization, different word order, or locale-specific phrasing.
345
+
346
+ ### Basic usage
347
+
348
+ Provide only the translations you want to override; the rest fall back to default English.
349
+
350
+ ```tsx
351
+ <BarChart
352
+ barValues={barData}
353
+ description="Progreso de habilidades"
354
+ maxRange={200}
355
+ minRange={0}
356
+ sortFns={['alphabetically', 'none']}
357
+ title="Habilidades"
358
+ translations={{
359
+ accessibility: {
360
+ gainedNowAt: 'ganado - ahora en',
361
+ inLabel: 'en',
362
+ inOnly: 'en ',
363
+ },
364
+ locale: 'es',
365
+ sortLabel: 'Ordenar por:',
366
+ sortOptions: {
367
+ labelAsc: 'Etiqueta (A-Z)',
368
+ labelDesc: 'Etiqueta (Z-A)',
369
+ none: 'Ninguno',
370
+ valueAsc: 'Valor (Bajo-Alto)',
371
+ valueDesc: 'Valor (Alto-Bajo)',
372
+ },
373
+ }}
374
+ />
375
+ ```
376
+
377
+ <Canvas of={BarChartStories.WithStringTranslations} />
378
+
379
+ ### Partial translations and locale
380
+
381
+ You can override a single key; the rest keep their defaults. The **locale** property controls number formatting (scale labels, value labels) via `Intl.NumberFormat`:
382
+
383
+ ```tsx
384
+ // Only change locale; sort and accessibility use defaults
385
+ translations={{ locale: 'de-DE' }}
386
+
387
+ // Override just sort label and locale
388
+ translations={{
389
+ locale: 'en-GB',
390
+ sortLabel: 'Sort by:',
391
+ }}
392
+ ```
393
+
394
+ ### Accessibility: strings vs functions
395
+
396
+ **Strings** — Use when the built-in template order fits your language. Fragments are plugged into the default pattern (e.g. `"{value} {unit} {inLabel} {yLabel}"`).
397
+
398
+ **Functions** — Use when you need full control. Each function receives context (`yLabel`, values, `unit`, `locale`) and returns the complete summary string.
399
+
400
+ <Canvas of={BarChartStories.WithFunctionTranslations} />
401
+
402
+ **Best practices:**
403
+
404
+ - Provide complete translations for the language your app uses; use partial overrides for one-off changes.
405
+ - Prefer string accessibility keys when the default order works; use functions for pluralization, word order, or locale-specific phrasing.
406
+ - Use appropriate locale codes (e.g. `'en-US'`, `'es-ES'`, `'fr-FR'`) for number formatting.
407
+ - Test accessibility summaries with screen readers in the target language.
408
+ - Reuse translation objects across BarChart instances where possible.
409
+
410
+ ## Playground
411
+
412
+ <Canvas sourceState="shown" of={BarChartStories.Default} />
413
+
414
+ <Controls />