Skip to content

Commit

Permalink
Add instant-open prop
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks committed Apr 17, 2024
1 parent cb47ddf commit ad72b3e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 22 deletions.
1 change: 1 addition & 0 deletions docs/pages/base-ui/api/use-tooltip.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
},
"required": true
},
"instantOpen": { "type": { "name": "boolean", "description": "boolean" }, "required": true },
"mounted": { "type": { "name": "boolean", "description": "boolean" }, "required": true },
"setAnchorEl": {
"type": {
Expand Down
60 changes: 39 additions & 21 deletions docs/pages/experiments/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ export const TooltipContent = styled(Tooltip.Content)`
&[data-transition] {
transition-property: opacity, transform;
transition-duration: 0.15s;
transition-duration: 0.2s;
opacity: 0;
transform: scale(0.9);
&[data-status='opening'] {
opacity: 1;
transform: scale(1);
&[data-instant-open] {
transition-duration: 0s;
}
}
&[data-status='closing'][data-instant-open] {
transition-duration: 0s;
}
}
Expand Down Expand Up @@ -84,26 +92,36 @@ export default function TooltipTransitionExperiment() {
fontFamily: '"IBM Plex Sans", sans-serif',
}}
>
<h2>Transition</h2>
<Tooltip.Group>
<Tooltip.Root>
<Tooltip.AnchorFragment>
<AnchorButton>Anchor</AnchorButton>
</Tooltip.AnchorFragment>
<TooltipContent data-transition sideOffset={7}>
Tooltip
</TooltipContent>
</Tooltip.Root>
<h2>Animation</h2>
<Tooltip.Root>
<Tooltip.AnchorFragment>
<AnchorButton>Anchor</AnchorButton>
</Tooltip.AnchorFragment>
<TooltipContent data-animation sideOffset={7}>
Tooltip
</TooltipContent>
</Tooltip.Root>
</Tooltip.Group>
<h2>Transition With Group</h2>
<div style={{ display: 'flex', gap: 5 }}>
<Tooltip.Group closeDelay={200}>
<Tooltip.Root>
<Tooltip.AnchorFragment>
<AnchorButton>Anchor</AnchorButton>
</Tooltip.AnchorFragment>
<TooltipContent data-transition sideOffset={7}>
Tooltip
</TooltipContent>
</Tooltip.Root>
<Tooltip.Root>
<Tooltip.AnchorFragment>
<AnchorButton>Anchor</AnchorButton>
</Tooltip.AnchorFragment>
<TooltipContent data-transition sideOffset={7}>
Tooltip
</TooltipContent>
</Tooltip.Root>
</Tooltip.Group>
</div>
<h2>Animation</h2>
<Tooltip.Root>
<Tooltip.AnchorFragment>
<AnchorButton>Anchor</AnchorButton>
</Tooltip.AnchorFragment>
<TooltipContent data-animation sideOffset={7}>
Tooltip
</TooltipContent>
</Tooltip.Root>
</div>
);
}
3 changes: 3 additions & 0 deletions docs/translations/api-docs/use-tooltip/use-tooltip.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
"floatingContext": { "description": "The Floating UI context of the tooltip element." },
"getAnchorProps": { "description": "Props to spread on the anchor element." },
"getContentProps": { "description": "Props to spread on the tooltip element." },
"instantOpen": {
"description": "If <code>true</code>, the tooltip is in a phase where it should be instantly open while in a group."
},
"mounted": {
"description": "Whether the tooltip is mounted, including CSS transitions or animations."
},
Expand Down
1 change: 1 addition & 0 deletions packages/mui-base/src/Tooltip/TooltipContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ const TooltipContent = React.forwardRef(function TooltipContent(
['data-side' as string]: tooltip.side,
['data-alignment' as string]: tooltip.alignment,
['data-status' as string]: tooltip.status,
['data-instant-open' as string]: tooltip.instantOpen || undefined,
...otherProps,
};

Expand Down
15 changes: 14 additions & 1 deletion packages/mui-base/src/useTooltip/useTooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,20 @@ export function useTooltip(params: UseTooltipParameters): UseTooltipReturnValue
return undefined;
}, [keepMounted, mounted, elements, update]);

const { delay: groupDelay, isInstantPhase, currentId } = useDelayGroup(context);

const renderedSide = getSide(renderedPlacement);
const renderedAlignment = getAlignment(renderedPlacement) || 'center';
const isHidden = hideWhenDetached && middlewareData.hide?.referenceHidden;

const { delay: groupDelay } = useDelayGroup(context);
let isInstantOpen = isInstantPhase;
if (isInstantPhase) {
if (open) {
isInstantOpen = true;
} else if (currentId === context.floatingId) {
isInstantOpen = false;
}
}

const hover = useHover(context, {
delay:
Expand Down Expand Up @@ -232,6 +241,7 @@ export function useTooltip(params: UseTooltipParameters): UseTooltipReturnValue
['data-side' as string]: renderedSide,
['data-alignment' as string]: renderedAlignment,
['data-status' as string]: status,
['data-instant-open' as string]: isInstantOpen || undefined,
style: {
...floatingStyles,
maxWidth: 'var(--available-width)',
Expand All @@ -255,6 +265,7 @@ export function useTooltip(params: UseTooltipParameters): UseTooltipReturnValue
status,
setMounted,
refs,
isInstantOpen,
],
);

Expand All @@ -270,6 +281,7 @@ export function useTooltip(params: UseTooltipParameters): UseTooltipReturnValue
floatingContext: context,
side: renderedSide,
alignment: renderedAlignment,
instantOpen: isInstantOpen,
}),
[
status,
Expand All @@ -281,6 +293,7 @@ export function useTooltip(params: UseTooltipParameters): UseTooltipReturnValue
context,
renderedSide,
renderedAlignment,
isInstantOpen,
],
);
}
4 changes: 4 additions & 0 deletions packages/mui-base/src/useTooltip/useTooltip.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ export interface UseTooltipReturnValue {
* The status of the tooltip element when considering CSS transitions or animations.
*/
status: Status;
/**
* If `true`, the tooltip is in a phase where it should be instantly open while in a group.
*/
instantOpen: boolean;
}

export interface UseTooltipOpenStateReturnValue {
Expand Down

0 comments on commit ad72b3e

Please sign in to comment.