-
Notifications
You must be signed in to change notification settings - Fork 49
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
Feature/tooltip component #190
Open
garronej
wants to merge
2
commits into
main
Choose a base branch
from
feature/tooltip-component
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React, { forwardRef, memo, ReactElement } from "react"; | ||
import type { Equals } from "tsafe"; | ||
import { assert } from "tsafe/assert"; | ||
import { symToStr } from "tsafe/symToStr"; | ||
import { useAnalyticsId } from "./tools/useAnalyticsId"; | ||
|
||
export type TooltipProps = TooltipProps.WithClickAction | TooltipProps.WithHoverAction; | ||
|
||
export namespace TooltipProps { | ||
export type Common = { | ||
description: string; | ||
id?: string; | ||
className?: string; | ||
}; | ||
|
||
export type WithClickAction = Common & { | ||
kind: "click"; | ||
children?: ReactElement | string; | ||
}; | ||
|
||
export type WithHoverAction = Common & { | ||
kind?: "hover"; | ||
children: ReactElement | string; | ||
}; | ||
} | ||
|
||
/** @see <https://components.react-dsfr.codegouv.studio/?path=/docs/components-tooltip> */ | ||
export const Tooltip = memo( | ||
forwardRef<HTMLSpanElement, TooltipProps>((props, ref) => { | ||
const { id: id_prop, className, description, kind, children, ...rest } = props; | ||
assert<Equals<keyof typeof rest, never>>(); | ||
|
||
const id = useAnalyticsId({ | ||
"defaultIdPrefix": "fr-tooltip", | ||
"explicitlyProvidedId": id_prop | ||
}); | ||
|
||
const displayChildren = ( | ||
children: ReactElement | string | undefined, | ||
id: string | ||
): ReactElement => { | ||
if (children === undefined) return <></>; | ||
return typeof children === "string" ? ( | ||
<span aria-describedby={id} id={`tooltip-owner-${id}`}> | ||
{children} | ||
</span> | ||
) : ( | ||
children && | ||
React.cloneElement(children, { | ||
"aria-describedby": id, | ||
"id": `tooltip-owner-${id}` | ||
}) | ||
); | ||
}; | ||
|
||
return ( | ||
<> | ||
{props.kind === "click" ? ( | ||
<span ref={ref}> | ||
{children === undefined ? ( | ||
<button | ||
className="fr-btn--tooltip fr-btn" | ||
aria-describedby={id} | ||
id={`tooltip-owner-${id}`} | ||
> | ||
Information contextuelle | ||
</button> | ||
) : ( | ||
displayChildren(children, id) | ||
)} | ||
<span | ||
className={`fr-tooltip fr-placement ${props.className}`} | ||
id={id} | ||
role="tooltip" | ||
aria-hidden="true" | ||
> | ||
{props.description} | ||
</span> | ||
</span> | ||
) : ( | ||
<span ref={ref}> | ||
{displayChildren(children, id)} | ||
<span | ||
className={`fr-tooltip fr-placement ${props.className}`} | ||
id={id} | ||
role="tooltip" | ||
aria-hidden="true" | ||
> | ||
{props.description} | ||
</span> | ||
</span> | ||
)} | ||
</> | ||
); | ||
}) | ||
); | ||
|
||
Tooltip.displayName = symToStr({ Tooltip }); | ||
|
||
export default Tooltip; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Tooltip, type TooltipProps } from "../dist/Tooltip"; | ||
import { sectionName } from "./sectionName"; | ||
import { getStoryFactory } from "./getStory"; | ||
import { assert, Equals } from "tsafe/assert"; | ||
|
||
const { meta, getStory } = getStoryFactory({ | ||
sectionName, | ||
"wrappedComponent": { Tooltip }, | ||
"description": ` | ||
- [See DSFR documentation](https://www.systeme-de-design.gouv.fr/elements-d-interface/composants/infobulle) | ||
- [See source code](https://github.com/codegouvfr/react-dsfr/blob/main/src/Tooltip.tsx)`, | ||
"argTypes": { | ||
"id": { | ||
"control": { "type": "text" }, | ||
"description": | ||
"Optional: tootlip Id, which is also use as aria-describedby for hovered/clicked element" | ||
}, | ||
"className": { | ||
"control": { "type": "text" }, | ||
"description": "Optional" | ||
}, | ||
"kind": { | ||
"control": { "type": "select" }, | ||
"options": (() => { | ||
const options = ["hover", "click"] as const; | ||
|
||
assert<Equals<typeof options[number] | undefined, TooltipProps["kind"]>>(); | ||
|
||
return options; | ||
})(), | ||
"description": "Optional." | ||
}, | ||
"description": { | ||
"control": { "type": "text" } | ||
}, | ||
"children": { | ||
"control": { "type": "text" } | ||
} | ||
}, | ||
"disabledProps": ["lang"] | ||
}); | ||
|
||
export default meta; | ||
|
||
const defaultOnHoverProps: TooltipProps.WithHoverAction = { | ||
"description": "lorem ipsum", | ||
"children": "Exemple" | ||
}; | ||
|
||
export const Default = getStory(defaultOnHoverProps); | ||
|
||
export const TooltipOnHover = getStory(defaultOnHoverProps); | ||
|
||
export const TooltipOnClick = getStory({ | ||
"kind": "click", | ||
"description": "lorem ipsum" | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Tooltip } from "../../src/Tooltip"; | ||
|
||
{ | ||
<Tooltip description="lorem ipsum">Exemple</Tooltip>; | ||
} | ||
{ | ||
<Tooltip kind="hover" description="lorem ipsum"> | ||
Exemple | ||
</Tooltip>; | ||
} | ||
{ | ||
<Tooltip kind="click" description="lorem ipsum" />; | ||
} | ||
{ | ||
<Tooltip kind="click" description="lorem ipsum"> | ||
Exemple | ||
</Tooltip>; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
should we add transation here ?
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.
should i fix the PR ?
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.
Yes you are welcome to!
This is not my work, I reopenned the PR because I merged it too rearly
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.
You can see my code review here: #176 (comment)