Skip to content

Commit

Permalink
feat: ✨ Create BackToTop component
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainlg committed Jun 12, 2023
1 parent 8a46988 commit 968379d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/BackToTop.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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;
right?: boolean;
};

export const BackToTop = memo(
forwardRef<HTMLAnchorElement, BackToTopProps>((props, ref) => {
const { t } = useTranslation();
const { anchor = "#top", right = false } = props;
return (
<div className={!right ? undefined : fr.cx("fr-grid-row", "fr-grid-row--right")}>
<a
ref={ref}
className={cx(fr.cx("fr-link", "fr-icon-arrow-up-fill", "fr-link--icon-left"))}
href={anchor}
>
{t("page top")}
</a>
</div>
);
})
);

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"
}
});
25 changes: 25 additions & 0 deletions stories/BackToTop.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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"
// },
// "right": {
// "control": { "type": null },
// "description": "Align to right"
// }
// },
"disabledProps": ["lang"]
});

export default meta;

export const Default = getStory({});

0 comments on commit 968379d

Please sign in to comment.