Skip to content

feat: ✨ Create BackToTop component - #139

Closed
sylvainlg wants to merge 2 commits into
codegouvfr:mainfrom
sylvainlg:feat/back-to-top
Closed

feat: ✨ Create BackToTop component#139
sylvainlg wants to merge 2 commits into
codegouvfr:mainfrom
sylvainlg:feat/back-to-top

Conversation

@sylvainlg

Copy link
Copy Markdown
Contributor

No description provided.

@garronej garronej left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your work, here is my review.

Comment thread src/BackToTop.tsx Outdated
const { t } = useTranslation();
const { anchor = "#top", right = false } = props;
return (
<div className={right ? cx(fr.cx("fr-grid-row", "fr-grid-row--right")) : ""}>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<div className={right ? cx(fr.cx("fr-grid-row", "fr-grid-row--right")) : ""}>
<div className={!right ? undefined : fr.cx("fr-grid-row", "fr-grid-row--right")}>

Comment thread src/BackToTop.tsx Outdated
const { useTranslation, addBackToTopTranslations } = createComponentI18nApi({
"componentName": symToStr({ BackToTop }),
"frMessages": {
"page_top": "Haut de page"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"page_top": "Haut de page"
"page top": "Haut de page"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need the keys to be valid variable names. They arent for other components.

Comment thread src/BackToTop.tsx
);
})
);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also want export default

Suggested change
export default BackToTop;

@sylvainlg
sylvainlg requested a review from garronej June 12, 2023 08:18

@garronej garronej left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your work, here is my review

Comment thread src/BackToTop.tsx
import { createComponentI18nApi } from "./i18n";

export type BackToTopProps = {
anchor?: string;

Copy link
Copy Markdown
Collaborator

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 valid href. We need to apply the fr-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.

Comment thread src/BackToTop.tsx

export type BackToTopProps = {
anchor?: string;
right?: boolean;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
right?: boolean;
/** Default: false (the back to top button is centered) */
right?: boolean;


export default meta;

export const Default = getStory({});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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.

@sylvainlg
sylvainlg requested a review from garronej June 13, 2023 07:50

@garronej garronej left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @sylvainlg,

This has to be adressed: #139 (comment)

@kevbarns

Copy link
Copy Markdown
Collaborator

@sylvainlg Cette PR est ouverte depuis juin 2023 avec trois revues « changes requested » de @garronej, dont la dernière du 08/07/2023 pointait un seul point restant : #139 (comment)

Ce point n'a jamais été traité. Pour mémoire, il demandait que sans ancre explicite, le comportement par défaut soit un scroll fluide vers le haut de page — ce qui impose un <button> stylé fr-link plutôt qu'un <a> sans href valide, et une ref plutôt qu'un sélecteur CSS, conformément au reste de la lib. Restaient aussi deux suggestions mécaniques : export default BackToTop et une story pour la variante alignée à droite.

@sylvainlg comptez-vous reprendre cette PR ? Si oui, elle a besoin d'un rebase sur main avant tout : trois ans se sont écoulés et le code environnant a changé.

Si vous n'avez plus la disponibilité, dites-le simplement — le composant reste utile et quelqu'un peut repartir de votre travail. @garronej dans ce cas, faut-il fermer cette PR et ouvrir une issue « Create BackToTop component » qui reprenne vos retours de 2023 ?

@sylvainlg

Copy link
Copy Markdown
Contributor Author

Bonjour

Oui désolé, j'ai changé de projet depuis, vous pouvez bien entendu reprendre ce qui est exploitable pour rendre disponible le composant avec les dernières évolutions de la lib.

@kevbarns

Copy link
Copy Markdown
Collaborator

Merci @sylvainlg, et merci d'avoir pris le temps de répondre après tout ce temps.

J'ai repris votre travail dans #516 : la structure, le nommage des props et vos deux stories y sont conservés. La seule remarque restée ouverte ici, #139 (comment), est traitée par une prop targetRef qui rend un <button type="button"> stylé fr-link, scrolle la cible et y déplace le focus.

Le défaut reste l'ancre #top de votre implémentation d'origine, et c'est délibéré : la doc DSFR précise que l'ancre est ce qui replace aussi le focus de navigation en haut de page, ce qu'un scroll scripté ne fait pas. Votre choix initial était donc le bon, l'ajout ne fait que couvrir les pages qui ne peuvent pas poser id="top" sur leur élément racine.

Je ferme cette PR au profit de #516. N'hésitez pas à y jeter un œil si le sujet vous intéresse encore.

@kevbarns kevbarns closed this Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants