Skip to content

Commit 9f5b04b

Browse files
committed
feat: ✨ Create BackToTop component
1 parent 8a46988 commit 9f5b04b

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/BackToTop.tsx

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 ? cx(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+
const { useTranslation, addBackToTopTranslations } = createComponentI18nApi({
34+
"componentName": symToStr({ BackToTop }),
35+
"frMessages": {
36+
"page_top": "Haut de page"
37+
}
38+
});
39+
40+
addBackToTopTranslations({
41+
lang: "en",
42+
messages: {
43+
"page_top": "Top of the page"
44+
}
45+
});

stories/BackToTop.stories.tsx

+25
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)