-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: ✨ Create BackToTop component #139
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -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; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
}; | ||||||||
|
||||||||
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> | ||||||||
); | ||||||||
}) | ||||||||
); | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also want export default
Suggested change
|
||||||||
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" | ||||||||
} | ||||||||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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({}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be best to have a story for when the back to top is to the right. |
||
export const BackToTopOnRight = getStory({ | ||
right: true | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If no anchor is explicitely provided the default behaviour shoud be to scroll sroll smoothly to the top of the page.
In this case you'll have to use a
<button />
component since it's not accible to use a<a />
without a validhref
. We need to apply thefr-link
class to the button to make it look a<a />
.Beside it's ok accept a css selector but the recomended approach should be to provide
ref
. Dsfr components do not allow to provide html id but all enable to provide ref.