Skip to content

Commit 968379d

Browse files
committed
feat: ✨ Create BackToTop component
1 parent 8a46988 commit 968379d

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/BackToTop.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from "react";
2+
import { forwardRef, memo } from "react";
3+
import { cx } from "./tools/cx";
4+
import { fr } from "./fr";
5+
import { symToStr } from "tsafe/symToStr";
6+
import { createComponentI18nApi } from "./i18n";
7+
8+
export type BackToTopProps = {
9+
anchor?: string;
10+
right?: boolean;
11+
};
12+
13+
export const BackToTop = memo(
14+
forwardRef<HTMLAnchorElement, BackToTopProps>((props, ref) => {
15+
const { t } = useTranslation();
16+
const { anchor = "#top", right = false } = props;
17+
return (
18+
<div className={!right ? undefined : fr.cx("fr-grid-row", "fr-grid-row--right")}>
19+
<a
20+
ref={ref}
21+
className={cx(fr.cx("fr-link", "fr-icon-arrow-up-fill", "fr-link--icon-left"))}
22+
href={anchor}
23+
>
24+
{t("page top")}
25+
</a>
26+
</div>
27+
);
28+
})
29+
);
30+
31+
BackToTop.displayName = symToStr({ BackToTop });
32+
33+
export default BackToTop;
34+
35+
const { useTranslation, addBackToTopTranslations } = createComponentI18nApi({
36+
"componentName": symToStr({ BackToTop }),
37+
"frMessages": {
38+
"page top": "Haut de page"
39+
}
40+
});
41+
42+
addBackToTopTranslations({
43+
lang: "en",
44+
messages: {
45+
"page top": "Top of the page"
46+
}
47+
});

stories/BackToTop.stories.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { BackToTop } from "../dist/BackToTop";
2+
import { getStoryFactory } from "./getStory";
3+
import { sectionName } from "./sectionName";
4+
5+
const { meta, getStory } = getStoryFactory({
6+
sectionName,
7+
"wrappedComponent": { BackToTop },
8+
"description": `- [See DSFR documentation](https://www.systeme-de-design.gouv.fr/elements-d-interface/composants/retour-en-haut-de-page/)
9+
- [See source code](https://github.com/codegouvfr/react-dsfr/blob/main/src/BackToTop.tsx)`,
10+
// "argTypes": {
11+
// "anchor": {
12+
// "control": { "type": null },
13+
// "defaultValue": "#top"
14+
// },
15+
// "right": {
16+
// "control": { "type": null },
17+
// "description": "Align to right"
18+
// }
19+
// },
20+
"disabledProps": ["lang"]
21+
});
22+
23+
export default meta;
24+
25+
export const Default = getStory({});

0 commit comments

Comments
 (0)