@@ -201,13 +201,157 @@ export const HeaderWithQuickAccessItems = getStory(
201201 "onSearchButtonClick" : undefined
202202 } ,
203203 {
204- "description" : `See [\\<Display \\/\\>](https://components.react-dsfr.codegouv.studio/?path=/docs/components-display) for instructions on how to integrate the Dark mode switch .
204+ "description" : `Let's see an example usage of quick access items in the Header component .
205205
206- Note for Next App Router: If you want to have \`quickAccessItems\` client side without having to wrap the whole \`<Header />\`
207- component within a \`"use client";\` directive you can use the \`<HeaderQuickAccessItem />\` component as demonstrated
208- [here](https://github.com/garronej/react-dsfr-next-appdir-demo/blob/b485bda99d6140e59584d3134ac9e203ae6b2208/app/layout.tsx#L72) and
209- [here](https://github.com/garronej/react-dsfr-next-appdir-demo/blob/b485bda99d6140e59584d3134ac9e203ae6b2208/app/LoginHeaderItem.tsx#L1-L24).
210- `
206+ \`src/Header.tsx\`
207+
208+ \`\`\`tsx
209+
210+ import { Header as DsfrHeader } from "@codegouvfr/react-dsfr/Header";
211+ import { LanguageSelect } from "./LanguageSelect";
212+ import { AuthButtons } from "./AuthButtons";
213+
214+ export function Header() {
215+
216+ return (
217+ <DsfrHeader
218+ quickAccessItems={[
219+ {
220+ iconId: "fr-icon-add-circle-line",
221+ text: "Créer un espace",
222+ linkProps: {
223+ "href": "#" // Link to a page
224+ }
225+ },
226+ {
227+ iconId: "fr-icon-mail-fill",
228+ linkProps: {
229+ href: "mailto:contact@code.gouv.fr"
230+ },
231+ text: "Contact us"
232+ },
233+ <LanguageSelect />, // See "LanguageSelect" component of this website
234+ headerFooterDisplayItem, // See "Display" component of this website
235+ <AuthButtons /> // See below
236+
237+ ]}
238+ />
239+ );
240+
241+ }
242+
243+ \`\`\`
244+
245+ If you need to create a dynamic Header quick action items there is how you can do it.
246+ Let's see an example with the \`AuthButton\` component.
247+ In this example we assume the use of [oidc-spa](https://oidc-spa.dev/) for authentication.
248+ And [i18nifty](for internationalization).
249+ You can see this component live [here](https://vite-insee-starter.demo-domain.ovh/).
250+
251+ \`src/AuthButton.tsx\`
252+
253+ \`\`\`tsx
254+
255+ import { HeaderQuickAccessItem } from "@codegouvfr/react-dsfr/Header";
256+ import { declareComponentKeys, useTranslation } from "i18n"; // i18nifty
257+ import { useOidc } from "oidc"; // oidc-spa
258+
259+ type Props = {
260+ // NOTE: If you component assigns id you must use the one passed as prop.
261+ // If you have multiple id you must prefix them to differentiate them.
262+ // In this example we don't actually need to set ids but I do is so you can see how to do it.
263+ // See this example where it's more relevant:
264+ id?: string;
265+ };
266+
267+ export function AuthButtons(props: Props) {
268+
269+ const { id } = props;
270+
271+ const { isUserLoggedIn, login, logout } = useOidc();
272+
273+ const { t } = useTranslation("AuthButtons");
274+
275+ if (!isUserLoggedIn) {
276+ return (
277+ <>
278+ <HeaderQuickAccessItem
279+ id={\`login-\${id}\`}
280+ quickAccessItem={{
281+ iconId: "fr-icon-lock-line",
282+ buttonProps: {
283+ onClick: () => login({ doesCurrentHrefRequiresAuth: false })
284+ },
285+ text: t("login")
286+ }}
287+ />
288+ <HeaderQuickAccessItem
289+ id={\`register-\${id}\`}
290+ quickAccessItem={{
291+ iconId: "ri-id-card-line",
292+ buttonProps: {
293+ onClick: () => login({
294+ doesCurrentHrefRequiresAuth: false,
295+ transformUrlBeforeRedirect: url => {
296+ const urlObj = new URL(url);
297+
298+ urlObj.pathname = urlObj.pathname.replace(
299+ /\\/auth$/,
300+ "/registrations"
301+ );
302+
303+ return urlObj.href;
304+ }
305+ })
306+ },
307+ text: t("register")
308+ }}
309+ />
310+ </>
311+ );
312+ }
313+
314+ return (
315+ <>
316+ <HeaderQuickAccessItem
317+ id={\`account-\${id}\`}
318+ quickAccessItem={{
319+ iconId: "fr-icon-account-fill",
320+ linkProps: {
321+ to: "/account"
322+ },
323+ text: t("my account")
324+ }}
325+ />
326+ <HeaderQuickAccessItem
327+ id={\`logout-\${id}\`}
328+ quickAccessItem={{
329+ iconId: "ri-logout-box-line",
330+ buttonProps: {
331+ onClick: () =>
332+ logout({
333+ redirectTo: "home"
334+ })
335+ },
336+ text: t("logout")
337+ }}
338+ />
339+ </>
340+ );
341+
342+ }
343+
344+ const { i18n } = declareComponentKeys<
345+ | "login"
346+ | "register"
347+ | "logout"
348+ | "my account"
349+ >()("AuthButtons");
350+
351+ export type I18n = typeof i18n;
352+ \`\`\`
353+
354+ `
211355 }
212356) ;
213357
0 commit comments