Skip to content

Commit

Permalink
Merge branch 'main' into colors-package-example
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Feb 8, 2023
2 parents 8f67c1e + e328f22 commit bb94f7c
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 1,078 deletions.
69 changes: 50 additions & 19 deletions packages/react/src/components/Toggletip/Toggletip.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,53 @@ export default {

export const Default = () => {
return (
<div style={{ display: 'flex', justifyContent: 'center' }}>
<ToggletipLabel>Toggletip label</ToggletipLabel>
<Toggletip defaultOpen>
<ToggletipButton label="Show information">
<Information />
</ToggletipButton>
<ToggletipContent>
<p>
Lorem ipsum dolor sit amet, di os consectetur adipiscing elit, sed
do eiusmod tempor incididunt ut fsil labore et dolore magna aliqua.
</p>
<ToggletipActions>
<Link href="#">Link action</Link>
<Button size="sm">Button</Button>
</ToggletipActions>
</ToggletipContent>
</Toggletip>
<div>
<div style={{ display: 'flex', alignItems: 'center' }}>
<ToggletipLabel>Toggletip label</ToggletipLabel>
<Toggletip>
<ToggletipButton label="Show information">
<Information />
</ToggletipButton>
<ToggletipContent>
<p>
Lorem ipsum dolor sit amet, di os consectetur adipiscing elit, sed
do eiusmod tempor incididunt ut fsil labore et dolore magna
aliqua.
</p>
<ToggletipActions>
<Link href="#">Link action</Link>
<Button size="sm">Button</Button>
</ToggletipActions>
</ToggletipContent>
</Toggletip>
</div>
<br />
<br />
<div
style={{
display: 'flex',
alignItems: 'center',
}}>
<ToggletipLabel>
Toggletip label -- using <code>defaultOpen</code> prop
</ToggletipLabel>
<Toggletip defaultOpen>
<ToggletipButton label="Show information">
<Information />
</ToggletipButton>
<ToggletipContent>
<p>
Lorem ipsum dolor sit amet, di os consectetur adipiscing elit, sed
do eiusmod tempor incididunt ut fsil labore et dolore magna
aliqua.
</p>
<ToggletipActions>
<Link href="#">Link action</Link>
<Button size="sm">Button</Button>
</ToggletipActions>
</ToggletipContent>
</Toggletip>
</div>
</div>
);
};
Expand All @@ -76,7 +106,9 @@ const PlaygroundStory = (controls) => {
const { align } = controls;
return (
<>
<ToggletipLabel>Toggletip label</ToggletipLabel>
<ToggletipLabel>
Toggletip label -- using <code>defaultOpen</code> prop
</ToggletipLabel>
<Toggletip align={align} defaultOpen>
<ToggletipButton label="Show information">
<Information />
Expand Down Expand Up @@ -130,7 +162,6 @@ Playground.story = {
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}>
{story()}
</div>
Expand Down
25 changes: 25 additions & 0 deletions packages/react/src/components/Toggletip/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,33 @@ function Toggletip({
function onKeyDown(event) {
if (open && match(event, keys.Escape)) {
actions.close();

// If the menu is closed while focus is still inside the menu, it should return to the trigger button (#12922)
const button = ref.current.children[0];
if (button && button.type === 'button') {
button.focus();
}
}
}

function handleBlur(event) {
// Do not close if the menu itself is clicked, should only close on focus out
if (open && event.relatedTarget === null) {
return;
}
if (!event.currentTarget.contains(event.relatedTarget)) {
// The menu should be closed when focus leaves the `Toggletip` (#12922)
actions.close();
}
}

// If the `Toggletip` is the last focusable item in the tab order, it shoudl also close when the browser window loses focus (#12922)
useWindowEvent('blur', () => {
if (open) {
actions.close();
}
});

useWindowEvent('click', (event) => {
if (open && !ref.current.contains(event.target)) {
actions.close();
Expand All @@ -115,6 +139,7 @@ function Toggletip({
highContrast
open={open}
onKeyDown={onKeyDown}
onBlur={handleBlur}
ref={ref}>
{children}
</Popover>
Expand Down
Loading

0 comments on commit bb94f7c

Please sign in to comment.