Skip to content

Commit e1050fc

Browse files
authored
Merge pull request #297 from codegouvfr/feature/add-pro-connect-button
Feature / Add ProConnect button
2 parents 7d2e6a6 + 6e9f525 commit e1050fc

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

src/ProConnectButton.tsx

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import React, { forwardRef, memo, type CSSProperties } from "react";
2+
import { symToStr } from "tsafe/symToStr";
3+
import { createComponentI18nApi } from "./i18n";
4+
import { fr } from "./fr";
5+
import { assert, type Equals } from "tsafe/assert";
6+
import { cx } from "./tools/cx";
7+
import { useAnalyticsId } from "./tools/useAnalyticsId";
8+
import "./assets/proconnect-btn.css";
9+
10+
export type ProConnectButtonProps =
11+
| ProConnectButtonProps.WithUrl
12+
| ProConnectButtonProps.WithOnClick;
13+
14+
export namespace ProConnectButtonProps {
15+
type Common = {
16+
id?: string;
17+
className?: string;
18+
/** Default: false */
19+
classes?: Partial<Record<"root" | "login" | "brand", string>>;
20+
style?: CSSProperties;
21+
};
22+
export type WithUrl = Common & {
23+
url: string;
24+
onClick?: never;
25+
};
26+
export type WithOnClick = Common & {
27+
url?: never;
28+
onClick: React.MouseEventHandler<HTMLButtonElement>;
29+
};
30+
}
31+
32+
/** @see <https://components.react-dsfr.codegouv.studio/?path=/docs/components-proconnectbutton> */
33+
export const ProConnectButton = memo(
34+
forwardRef<HTMLDivElement, ProConnectButtonProps>((props, ref) => {
35+
const { classes = {}, className, url: href, style, onClick, id: id_props, ...rest } = props;
36+
37+
assert<Equals<keyof typeof rest, never>>();
38+
39+
const id = useAnalyticsId({
40+
"defaultIdPrefix": "fr-proconnect-button",
41+
"explicitlyProvidedId": id_props
42+
});
43+
44+
const { t } = useTranslation();
45+
46+
const Inner = onClick !== undefined ? "button" : "a";
47+
const innerProps = (onClick !== undefined ? { onClick } : { href }) as any;
48+
49+
return (
50+
<div
51+
id={id}
52+
className={cx(fr.cx("fr-connect-group"), classes.root, className)}
53+
style={style}
54+
ref={ref}
55+
>
56+
<Inner className={fr.cx("fr-btn", "fr-connect")} {...innerProps}>
57+
<span className={cx(fr.cx("fr-connect__login"), classes.login)}>
58+
S’identifier avec
59+
</span>
60+
<span className={cx(fr.cx("fr-connect__brand"), classes.brand)}>
61+
ProConnect
62+
</span>
63+
</Inner>
64+
<p>
65+
<a
66+
href={"https://proconnect.gouv.fr/"}
67+
target="_blank"
68+
rel="noopener"
69+
title={`${t("what is service")} - ${t("new window")}`}
70+
>
71+
{t("what is service")}
72+
</a>
73+
</p>
74+
</div>
75+
);
76+
})
77+
);
78+
79+
ProConnectButton.displayName = symToStr({ ProConnectButton });
80+
81+
export default ProConnectButton;
82+
83+
const { useTranslation, addProConnectButtonTranslations } = createComponentI18nApi({
84+
"componentName": symToStr({ ProConnectButton }),
85+
"frMessages": {
86+
/* spell-checker: disable */
87+
"what is service": "Qu’est-ce que ProConnect ?",
88+
"new window": "nouvelle fenêtre"
89+
/* spell-checker: enable */
90+
}
91+
});
92+
93+
addProConnectButtonTranslations({
94+
"lang": "en",
95+
"messages": {
96+
"what is service": "What's ProConnect?",
97+
"new window": "new window"
98+
}
99+
});
100+
101+
export { addProConnectButtonTranslations };

src/assets/proconnect-btn.css

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stories/ProConnectButton.stories.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ProConnectButton } from "../dist/ProConnectButton";
2+
import { sectionName } from "./sectionName";
3+
import { getStoryFactory, logCallbacks } from "./getStory";
4+
5+
const { meta, getStory } = getStoryFactory({
6+
sectionName,
7+
"wrappedComponent": { ProConnectButton },
8+
"description": `
9+
- [See DSFR documentation]https://github.com/numerique-gouv/agentconnect-documentation/blob/main/doc_fs/bouton_proconnect.md)
10+
- [See source code](https://github.com/codegouvfr/react-dsfr/blob/main/src/ProConnectButton.tsx)`
11+
});
12+
13+
export default meta;
14+
15+
export const Default = getStory({
16+
"url": "https://example.com"
17+
});
18+
19+
export const WithOnClick = getStory({
20+
...logCallbacks(["onClick"])
21+
});

0 commit comments

Comments
 (0)