-
Notifications
You must be signed in to change notification settings - Fork 85
feat(storybook): upgrade from v6 to v10 #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b9159e7
upgrade from 6 to 8
lsagetlethias 4eddd8a
upgrade from 8 to 9
lsagetlethias 0a430b0
upgrade from 9 to 10
lsagetlethias f2be923
unstyled tweak and fix picto
lsagetlethias 851e9e4
fix story order
lsagetlethias 4c162f3
remove unused import
lsagetlethias 808e784
handle dsfr dark theme during sb loading
lsagetlethias 994e57d
feat: upgrade storybook and add ToC
lsagetlethias 2284f31
feat: toc as dsfr side menu
lsagetlethias 8ef98a5
Merge branch 'main' of github.com:codegouvfr/react-dsfr into feat/upg…
lsagetlethias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,4 @@ CHANGELOG.md | |
| .yarn_home/ | ||
| /test/integration/ | ||
| /storybook-static/ | ||
|
|
||
| !.storybook | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import React from "react"; | ||
| import { Stories as BaseStories } from "@storybook/addon-docs/blocks"; | ||
| import { type PropsOf } from "@emotion/react"; | ||
|
|
||
| export const Stories = (props: PropsOf<typeof BaseStories>) => { | ||
| return <BaseStories {...props} />; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import { styled } from "storybook/theming"; | ||
| import SideMenu, { SideMenuProps } from "../dist/SideMenu"; | ||
| import Channel from "storybook/internal/channels"; | ||
| import { NAVIGATE_URL } from "storybook/internal/core-events"; | ||
| import React, { useEffect, useState } from "react"; | ||
| import { DocsTypes } from "@storybook/addon-docs"; | ||
|
|
||
| export type TocType = Exclude<Required<DocsTypes["parameters"]>["docs"]["toc"], undefined>; | ||
|
|
||
| const Aside = styled.div` | ||
| position: fixed; | ||
| right: 4rem; | ||
| top: 0; | ||
| bottom: 0; | ||
| width: 16rem; | ||
| z-index: 1; | ||
| overflow: auto; | ||
| padding-top: 4rem; | ||
| padding-bottom: 2rem; | ||
| padding-left: 1rem; | ||
| padding-right: 1rem; | ||
|
|
||
| -webkit-font-smoothing: antialiased; | ||
| -moz-osx-font-smoothing: grayscale; | ||
| -webkit-tap-highlight-color: rgba(0, 0, 0, 0); | ||
| -webkit-overflow-scrolling: touch; | ||
|
|
||
| @media (max-width: 768px) { | ||
| display: none; | ||
| } | ||
|
|
||
| & .fr-sidemenu__inner { | ||
| padding: 0; | ||
| } | ||
|
|
||
| & .fr-sidemenu__link { | ||
| padding: 0.5rem 0.75rem; | ||
| } | ||
| `; | ||
|
|
||
| /** | ||
| * Hook pour détecter le heading actuellement visible avec IntersectionObserver | ||
| */ | ||
| function useActiveHeading(headings: HTMLHeadingElement[]) { | ||
| const [activeId, setActiveId] = useState<string>(""); | ||
|
|
||
| useEffect(() => { | ||
| if (headings.length === 0) return; | ||
|
|
||
| // Map pour stocker les ratios d'intersection de chaque heading | ||
| const headingObservers = new Map<string, number>(); | ||
|
|
||
| const observer = new IntersectionObserver( | ||
| entries => { | ||
| // Mettre à jour le ratio d'intersection pour chaque heading observé | ||
| entries.forEach(entry => { | ||
| const id = entry.target.id; | ||
| if (entry.isIntersecting) { | ||
| headingObservers.set(id, entry.intersectionRatio); | ||
| } else { | ||
| headingObservers.set(id, 0); | ||
| } | ||
| }); | ||
|
|
||
| // Trouver le heading avec le plus grand ratio d'intersection | ||
| let maxRatio = 0; | ||
| let activeHeadingId = ""; | ||
|
|
||
| headingObservers.forEach((ratio, id) => { | ||
| if (ratio > maxRatio) { | ||
| maxRatio = ratio; | ||
| activeHeadingId = id; | ||
| } | ||
| }); | ||
|
|
||
| // Ne mettre à jour que si on a trouvé un heading visible | ||
| // Sinon on garde l'état précédent | ||
| if (activeHeadingId && activeHeadingId !== activeId) { | ||
| setActiveId(activeHeadingId); | ||
| } else if (!activeId && headings.length > 0) { | ||
| // Cas initial : si aucun heading n'est actif encore, prendre le premier | ||
| setActiveId(headings[0].id); | ||
| } | ||
| }, | ||
| { | ||
| // rootMargin négatif = créer une zone "active" au centre du viewport | ||
| // "-20% 0px -35% 0px" = zone active entre 20% du haut et 65% du bas | ||
| rootMargin: "-20% 0px -35% 0px", | ||
| threshold: [0, 0.25, 0.5, 0.75, 1] // Observer à différents niveaux de visibilité | ||
| } | ||
| ); | ||
|
|
||
| // Observer tous les headings | ||
| headings.forEach(heading => { | ||
| if (heading.id) { | ||
| observer.observe(heading); | ||
| headingObservers.set(heading.id, 0); | ||
| } | ||
| }); | ||
|
|
||
| return () => { | ||
| observer.disconnect(); | ||
| }; | ||
| }, [headings, activeId]); | ||
|
|
||
| return activeId; | ||
| } | ||
|
|
||
| interface TableOfContentsCustomProps { | ||
| channel: Channel; | ||
| } | ||
|
|
||
| export const TableOfContentsCustom = ({ channel }: TableOfContentsCustomProps) => { | ||
| const [headingElements, setHeadingElements] = useState<HTMLHeadingElement[]>([]); | ||
|
|
||
| // Initialiser les headings une seule fois | ||
| useEffect(() => { | ||
| const contentElement = document.querySelector(".sbdocs-content"); | ||
| const elements = Array.from( | ||
| contentElement?.querySelectorAll<HTMLHeadingElement>( | ||
| "h3:not(.docs-story *, .skip-toc)" | ||
| ) ?? [] | ||
| ); | ||
| setHeadingElements(elements); | ||
| }, []); | ||
|
|
||
| // Utiliser le hook pour tracker l'ID actif | ||
| const activeId = useActiveHeading(headingElements); | ||
|
|
||
| // Créer les items avec isActive | ||
| const headings = headingElements.map<SideMenuProps.Item>(heading => ({ | ||
| text: (heading.innerText || heading.textContent).trim(), | ||
| isActive: heading.id === activeId, | ||
| linkProps: { | ||
| href: `#${heading.id}`, | ||
| onClick(e) { | ||
| e.preventDefault(); | ||
| if (e.currentTarget instanceof HTMLAnchorElement) { | ||
| const [, headerId] = e.currentTarget.href.split("#"); | ||
| if (headerId) { | ||
| channel.emit(NAVIGATE_URL, { url: `#${headerId}` }); | ||
| document.querySelector(`#${heading.id}`)?.scrollIntoView({ | ||
| behavior: "smooth" | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| })); | ||
|
|
||
| return ( | ||
| <Aside> | ||
| <SideMenu align="right" burgerMenuButtonText="Table des matières" items={headings} /> | ||
| </Aside> | ||
| ); | ||
| }; |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { create } from "storybook/theming"; | ||
|
|
||
| const brandImage = "logo.png"; | ||
| const brandTitle = "@codegouvfr/react-dsfr"; | ||
| const brandUrl = "https://github.com/codegouvfr/react-dsfr"; | ||
| const fontBase = '"Marianne", arial, sans-serif'; | ||
| const fontCode = "monospace"; | ||
|
|
||
| export const darkTheme = create({ | ||
| base: "dark", | ||
| appBg: "#1E1E1E", | ||
| appContentBg: "#161616", | ||
| barBg: "#161616", | ||
| colorSecondary: "#8585F6", | ||
| textColor: "#FFFFFF", | ||
| brandImage, | ||
| brandTitle, | ||
| brandUrl, | ||
| fontBase, | ||
| fontCode | ||
| }); | ||
|
|
||
| export const lightTheme = create({ | ||
| base: "light", | ||
| appBg: "#F6F6F6", | ||
| appContentBg: "#FFFFFF", | ||
| barBg: "#FFFFFF", | ||
| colorSecondary: "#000091", | ||
| textColor: "#212121", | ||
| brandImage, | ||
| brandTitle, | ||
| brandUrl, | ||
| fontBase, | ||
| fontCode | ||
| }); | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { defineMain } from "@storybook/react-vite/node"; | ||
|
|
||
| export default defineMain({ | ||
| framework: "@storybook/react-vite", | ||
| features: { | ||
| backgrounds: false | ||
| }, | ||
| stories: [ | ||
| "../stories/*.mdx", | ||
| "../stories/*.stories.@(ts|tsx)", | ||
| "../stories/blocks/*.stories.@(ts|tsx)", | ||
| "../stories/charts/*.stories.@(ts|tsx)" | ||
| ], | ||
| addons: [ | ||
| "@vueless/storybook-dark-mode", | ||
| "@storybook/addon-links", | ||
lsagetlethias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "@storybook/addon-a11y", | ||
| "@storybook/addon-docs" | ||
| ], | ||
| staticDirs: ["../dist", "./static"] | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.