diff --git a/src/BackToTop.tsx b/src/BackToTop.tsx new file mode 100644 index 00000000..55dda119 --- /dev/null +++ b/src/BackToTop.tsx @@ -0,0 +1,48 @@ +import React from "react"; +import { forwardRef, memo } from "react"; +import { cx } from "./tools/cx"; +import { fr } from "./fr"; +import { symToStr } from "tsafe/symToStr"; +import { createComponentI18nApi } from "./i18n"; + +export type BackToTopProps = { + anchor?: string; + /** Default: false (the back to top button is centered) */ + right?: boolean; +}; + +export const BackToTop = memo( + forwardRef((props, ref) => { + const { t } = useTranslation(); + const { anchor = "#top", right = false } = props; + return ( +
+ + {t("page top")} + +
+ ); + }) +); + +BackToTop.displayName = symToStr({ BackToTop }); + +export default BackToTop; + +const { useTranslation, addBackToTopTranslations } = createComponentI18nApi({ + "componentName": symToStr({ BackToTop }), + "frMessages": { + "page top": "Haut de page" + } +}); + +addBackToTopTranslations({ + lang: "en", + messages: { + "page top": "Top of the page" + } +}); diff --git a/stories/BackToTop.stories.tsx b/stories/BackToTop.stories.tsx new file mode 100644 index 00000000..d3abe1aa --- /dev/null +++ b/stories/BackToTop.stories.tsx @@ -0,0 +1,30 @@ +import { BackToTop } from "../dist/BackToTop"; +import { getStoryFactory } from "./getStory"; +import { sectionName } from "./sectionName"; + +const { meta, getStory } = getStoryFactory({ + sectionName, + "wrappedComponent": { BackToTop }, + "description": `- [See DSFR documentation](https://www.systeme-de-design.gouv.fr/elements-d-interface/composants/retour-en-haut-de-page/) +- [See source code](https://github.com/codegouvfr/react-dsfr/blob/main/src/BackToTop.tsx)`, + "argTypes": { + "anchor": { + "control": { "type": null }, + "defaultValue": "#top", + "description": "The #anchor for the corresponding HTML id" + }, + "right": { + "control": "boolean", + "defaultValue": false, + "description": "Align to right" + } + }, + "disabledProps": ["lang"] +}); + +export default meta; + +export const Default = getStory({}); +export const BackToTopOnRight = getStory({ + right: true +});