@plexui/ui 0.59.0 → 0.59.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,8 +6,11 @@ import { createContext, useContext, useRef, useState } from "react";
6
6
  import { useTimeout } from "usehooks-ts";
7
7
  import { useEscCloseStack } from "../../hooks/useEscCloseStack.js";
8
8
  import { preventDefaultHandler } from "../../lib/helpers.js";
9
- import { isHoverPointer } from "../../lib/inputMode.js";
9
+ import { isFocusVisible, isHoverPointer, notePointerInput, trackInputModality } from "../../lib/inputMode.js";
10
10
  import s from "@plexui/styles/components/Tooltip/Tooltip.module.css";
11
+ // A tooltip opens on a focus that shows its ring only (isFocusVisible): it follows the last input of the page
12
+ if (typeof document !== "undefined")
13
+ trackInputModality(document);
11
14
  export const Tooltip = (props) => {
12
15
  const { ref: forwardedRef, children, content, forceOpen = content === null ? false : undefined, maxWidth = 300, openDelay = 150, interactive = false, compact = false, preventUnintentionalClickToClose, align, alignOffset = 0, side, sideOffset = 5, gutterSize = "md", contentClassName, onPointerDown, onClick, ...restProps } = props;
13
16
  const [visible, setVisible] = useState(false);
@@ -71,6 +74,8 @@ export const Trigger = ({ children, asChild = true, onPointerMove, onPointerLeav
71
74
  const cancelledPointer = useRef(false);
72
75
  return (_jsx(RadixTooltip.Trigger, { asChild: asChild, ...restProps, ...(focusRecovery?.state && { "data-state": focusRecovery.state }), onPointerDown: (event) => {
73
76
  cancelledPointer.current = false;
77
+ // A trigger in another document (an iframe) tracks that document's input
78
+ notePointerInput(event.currentTarget.ownerDocument);
74
79
  onPointerDown?.(event);
75
80
  }, onPointerCancel: (event) => {
76
81
  // Radix only clears its pointer-down flag on pointerup. A cancelled
@@ -78,12 +83,18 @@ export const Trigger = ({ children, asChild = true, onPointerMove, onPointerLeav
78
83
  cancelledPointer.current = true;
79
84
  onPointerCancel?.(event);
80
85
  }, onFocus: (event) => {
86
+ trackInputModality(event.currentTarget.ownerDocument);
81
87
  onFocus?.(event);
82
88
  if (event.defaultPrevented)
83
89
  return;
84
90
  if (event.currentTarget.hasAttribute("data-menu-pointer-focus")) {
85
91
  event.preventDefault();
86
92
  }
93
+ else if (!isFocusVisible(event.currentTarget)) {
94
+ // A focus after a mouse press that a script moved here (a dialog closed with the mouse returns focus to its
95
+ // trigger) shows no ring, so no tooltip: it stayed open until the focus left
96
+ event.preventDefault();
97
+ }
87
98
  else if (cancelledPointer.current) {
88
99
  event.preventDefault();
89
100
  focusRecovery?.recover();
@@ -95,6 +106,7 @@ export const Trigger = ({ children, asChild = true, onPointerMove, onPointerLeav
95
106
  event.preventDefault();
96
107
  }
97
108
  }, onPointerMove: (event) => {
109
+ trackInputModality(event.currentTarget.ownerDocument);
98
110
  onPointerMove?.(event);
99
111
  if (!isHoverPointer(event))
100
112
  event.preventDefault();
@@ -1 +1 @@
1
- {"version":3,"file":"Tooltip.js","sourceRoot":"","sources":["../../../../src/components/Tooltip/Tooltip.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;;AAEZ,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,IAAI,EAAE,MAAM,UAAU,CAAA;AACxD,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAoD,MAAM,OAAO,CAAA;AACrH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAA;AAC/D,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAA;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,CAAC,MAAM,sDAAsD,CAAA;AAkEpE,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,KAAmB,EAAE,EAAE;IAC7C,MAAM,EACJ,GAAG,EAAE,YAAY,EACjB,QAAQ,EACR,OAAO,EACP,SAAS,GAAG,OAAO,KAAK,IAAI,CAAC,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CAAC,CAAC,QAAQ,GACvC,CACb,CAAA;AACH,CAAC,CAAA;AAED,mEAAmE;AACnE,wDAAwD;AACxD,OAAO,CAAC,IAAI,GAAG,IAAI,CAAA;AACnB,OAAO,CAAC,OAAO,GAAG,OAAO,CAAA;AACzB,OAAO,CAAC,OAAO,GAAG,OAAO,CAAA;AACzB,iEAAiE;AACjE,sDAAsD;AACtD,mGAAmG;AACnG,OAAO,CAAC,gBAAgB,GAAG,gBAAgB,CAAA","sourcesContent":["\"use client\"\n\nimport clsx from \"clsx\"\nimport { Tooltip as RadixTooltip, Slot } from \"radix-ui\"\nimport { createContext, useContext, useRef, useState, type MouseEventHandler, type PointerEventHandler } from \"react\"\nimport { useTimeout } from \"usehooks-ts\"\nimport { useEscCloseStack } from \"../../hooks/useEscCloseStack\"\nimport { preventDefaultHandler } from \"../../lib/helpers\"\nimport { isHoverPointer } from \"../../lib/inputMode\"\nimport s from \"@plexui/styles/components/Tooltip/Tooltip.module.css\"\n\nexport type TooltipProps = {\n children: React.ReactNode\n /**\n * The content of the tooltip. If `null`, the tooltip will not render.\n */\n content: React.ReactNode\n /**\n * Defines the `max-width` of the tooltip content. `\"none\"` creates a single-line, naturally sized tooltip.\n * @default 300\n */\n maxWidth?: number | \"none\"\n /**\n * Forces the tooltip to remain open or closed\n */\n forceOpen?: boolean\n /**\n * Delay of when the tooltip is shown from first interaction, in milliseconds.\n * @default 150\n */\n openDelay?: number\n /** Indicates that the tooltip has interactive content, and should remain open when hovered. */\n interactive?: boolean\n /**\n * Short, 1-3 word tooltips, stylized inversely from normal tooltips\n */\n compact?: boolean\n /**\n * Prevents the tooltip from closing when the trigger is clicked right after opening\n * @default false\n */\n preventUnintentionalClickToClose?: true\n /**\n * The preferred alignment against the trigger. May change when collisions occur.\n * @default center\n */\n align?: RadixTooltip.TooltipContentProps[\"align\"]\n /**\n * An offset in pixels from the \"start\" or \"end\" alignment options.\n * @default 0\n */\n alignOffset?: RadixTooltip.TooltipContentProps[\"alignOffset\"]\n /**\n * The preferred side of the trigger to render against when open. Will be reversed when collisions occur.\n * @default top\n */\n side?: RadixTooltip.TooltipContentProps[\"side\"]\n /**\n * The distance in pixels from the trigger.\n * @default 5\n */\n sideOffset?: RadixTooltip.TooltipContentProps[\"sideOffset\"]\n /**\n * Gutter sizing inside the tooltip content\n * @default md\n */\n gutterSize?: \"sm\" | \"md\" | \"lg\"\n /** Ref for the tooltip */\n ref?: React.Ref<HTMLDivElement | null>\n onPointerDown?: PointerEventHandler\n onClick?: MouseEventHandler\n /** Optional class name to apply to the tooltip content */\n contentClassName?: string\n}\n\nexport const Tooltip = (props: TooltipProps) => {\n const {\n ref: forwardedRef,\n children,\n content,\n forceOpen = content === null ? false : undefined,\n maxWidth = 300,\n openDelay = 150,\n interactive = false,\n compact = false,\n preventUnintentionalClickToClose,\n align,\n alignOffset = 0,\n side,\n sideOffset = 5,\n gutterSize = \"md\",\n contentClassName,\n onPointerDown,\n onClick,\n ...restProps\n } = props\n const [visible, setVisible] = useState<boolean>(false)\n const [temporarilyPreventClickToClose, setTemporarilyPreventClickToClose] =\n useState<boolean>(false)\n useTimeout(\n () => setTemporarilyPreventClickToClose(false),\n temporarilyPreventClickToClose ? 400 : null,\n )\n const open = forceOpen ?? visible\n\n const handleOpenChange = (nextState: boolean) => {\n // When forceOpen is passed, don't manage internal state\n if (typeof forceOpen === \"boolean\") return\n\n setVisible(nextState)\n\n if (preventUnintentionalClickToClose) {\n setTemporarilyPreventClickToClose(nextState)\n }\n }\n\n const maybePreventClickClose = (evt: React.MouseEvent) => {\n if (preventUnintentionalClickToClose && temporarilyPreventClickToClose) {\n // Prevents tooltip listeners from closing\n evt.preventDefault()\n evt.stopPropagation()\n }\n }\n\n return (\n <Root\n open={open}\n delayDuration={openDelay}\n onOpenChange={handleOpenChange}\n disableHoverableContent={!interactive}\n >\n <Trigger asChild>\n <Slot.Root\n {...restProps}\n ref={forwardedRef}\n onPointerDown={(evt) => {\n maybePreventClickClose(evt)\n onPointerDown?.(evt)\n }}\n onClick={(evt) => {\n maybePreventClickClose(evt)\n onClick?.(evt)\n }}\n >\n {children}\n </Slot.Root>\n </Trigger>\n <Content\n maxWidth={maxWidth}\n compact={compact}\n align={align}\n alignOffset={alignOffset}\n side={side}\n sideOffset={sideOffset}\n gutterSize={gutterSize}\n className={contentClassName}\n >\n {content}\n </Content>\n </Root>\n )\n}\n\nconst RecoverFocusContext = createContext<{\n recover: () => void\n state: \"instant-open\" | undefined\n} | undefined>(undefined)\n\nconst Root = ({\n children,\n open,\n onOpenChange,\n ...restProps\n}: RadixTooltip.TooltipProps & {\n open: boolean\n onOpenChange: (nextState: boolean) => void\n}) => {\n const [recoveredFocus, setRecoveredFocus] = useState(false)\n const handleOpenChange = (nextState: boolean) => {\n setRecoveredFocus(false)\n onOpenChange(nextState)\n }\n useEscCloseStack(open, () => handleOpenChange(false))\n\n return (\n <RadixTooltip.Provider>\n <RadixTooltip.Root open={open} onOpenChange={handleOpenChange} {...restProps}>\n <RecoverFocusContext.Provider value={{\n recover: () => {\n if (open) return\n // Match a keyboard open even if Radix retained its cancelled\n // pointer flag and a previous hover's delayed-open state.\n document.dispatchEvent(new CustomEvent(\"tooltip.open\"))\n setRecoveredFocus(true)\n onOpenChange(true)\n },\n state: open && recoveredFocus ? \"instant-open\" : undefined,\n }}>\n {children}\n </RecoverFocusContext.Provider>\n </RadixTooltip.Root>\n </RadixTooltip.Provider>\n )\n}\n\ntype TooltipContentProps = RadixTooltip.TooltipContentProps & {\n children: React.ReactNode\n /**\n * Defines the `max-width` of the tooltip content. `\"none\"` creates a single-line, naturally sized tooltip.\n * @default 300\n */\n maxWidth?: number | \"none\"\n /**\n * Short, 1-3 word tooltips, stylized inversely from normal tooltips\n */\n compact?: boolean\n /**\n * Gutter sizing inside the tooltip content\n * @default md\n */\n gutterSize?: \"sm\" | \"md\" | \"lg\"\n /** Determines if the tooltip content should respond to click events */\n clickable?: boolean\n}\n\nexport const Content = ({\n children,\n maxWidth = 300,\n compact = false,\n clickable = undefined,\n alignOffset = 0,\n sideOffset = 5,\n gutterSize = \"md\",\n className,\n style,\n ...restProps\n}: TooltipContentProps) => {\n const focusRecovery = useContext(RecoverFocusContext)\n return (\n <RadixTooltip.Portal>\n <RadixTooltip.Content\n {...restProps}\n {...(focusRecovery?.state && { \"data-state\": focusRecovery.state })}\n className={clsx(s.Tooltip, className)}\n data-compact={compact}\n data-clickable={clickable}\n data-gutter-size={gutterSize}\n alignOffset={alignOffset}\n sideOffset={sideOffset}\n collisionPadding={15}\n hideWhenDetached\n style={{\n ...style,\n maxWidth,\n }}\n onEscapeKeyDown={preventDefaultHandler}\n >\n {children}\n </RadixTooltip.Content>\n </RadixTooltip.Portal>\n )\n}\n\ntype TooltipTriggerProps = RadixTooltip.TooltipTriggerProps & {\n /**\n * Determines if `tabindex=0` is applied to the trigger\n * @default true\n */\n focusable?: boolean\n /**\n * Optional class name to apply to the trigger\n */\n className?: string\n /** Ref for the trigger */\n ref?: React.Ref<HTMLSpanElement | null>\n}\n\nexport const Trigger = ({\n children,\n asChild = true,\n onPointerMove,\n onPointerLeave,\n onPointerDown,\n onPointerCancel,\n onFocus,\n onBlur,\n ...restProps\n}: RadixTooltip.TooltipTriggerProps) => {\n const focusRecovery = useContext(RecoverFocusContext)\n const cancelledPointer = useRef(false)\n\n return (\n <RadixTooltip.Trigger\n asChild={asChild}\n {...restProps}\n {...(focusRecovery?.state && { \"data-state\": focusRecovery.state })}\n onPointerDown={(event) => {\n cancelledPointer.current = false\n onPointerDown?.(event)\n }}\n onPointerCancel={(event) => {\n // Radix only clears its pointer-down flag on pointerup. A cancelled\n // touch gesture has no pointerup, so recover the next keyboard focus.\n cancelledPointer.current = true\n onPointerCancel?.(event)\n }}\n onFocus={(event) => {\n onFocus?.(event)\n if (event.defaultPrevented) return\n if (event.currentTarget.hasAttribute(\"data-menu-pointer-focus\")) {\n event.preventDefault()\n } else if (cancelledPointer.current) {\n event.preventDefault()\n focusRecovery?.recover()\n }\n }}\n onBlur={(event) => {\n onBlur?.(event)\n // Clipboard fallback borrows focus synchronously, then restores it.\n if (event.relatedTarget instanceof Element && event.relatedTarget.hasAttribute(\"data-plex-clipboard-fallback\")) {\n event.preventDefault()\n }\n }}\n onPointerMove={(event) => {\n onPointerMove?.(event)\n if (!isHoverPointer(event)) event.preventDefault()\n }}\n onPointerLeave={(event) => {\n onPointerLeave?.(event)\n if (!isHoverPointer(event)) event.preventDefault()\n }}\n >\n {children}\n </RadixTooltip.Trigger>\n )\n}\n\n// Optional wrapper for tooltip trigger, if the visual content isn't already accessible or with built-in visual indications\nexport const TriggerDecorator = (props: TooltipTriggerProps) => {\n const { children, className, focusable = true, ref, ...restProps } = props\n const isPlainText = typeof children === \"string\"\n\n return (\n <Slot.Root\n ref={ref}\n {...restProps}\n className={clsx(s.TriggerDecorator, className)}\n tabIndex={focusable ? 0 : undefined}\n >\n {isPlainText ? <span>{children}</span> : children}\n </Slot.Root>\n )\n}\n\n// Building advanced tooltip behaviors can be achieved by using the\n// composed components, and wiring up bespoke behaviors.\nTooltip.Root = Root\nTooltip.Content = Content\nTooltip.Trigger = Trigger\n// Basic <Tooltip> usage can be done with the component directly,\n// without requiring controlled component composition.\n// <TriggerDecorator> is a helper component that creates an accessible trigger with a visual effect\nTooltip.TriggerDecorator = TriggerDecorator\n"]}
1
+ {"version":3,"file":"Tooltip.js","sourceRoot":"","sources":["../../../../src/components/Tooltip/Tooltip.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;;AAEZ,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,IAAI,EAAE,MAAM,UAAU,CAAA;AACxD,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAoD,MAAM,OAAO,CAAA;AACrH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAA;AAC/D,OAAO,EAAE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AAc,EAAE,CAAA;QACpD,CAAC,EACD,cAAc,EAAE,CAAC,KAAK,EAAE,EAAE;YACxB,cAAc,EAAE,CAAC,KAAK,CAAC,CAAA;YACvB,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC;gBAAE,KAAK,CAAC,cAAc,EAAE,CAAA;QACpD,CAAC,YAEA,QAAQ,GACY,CACxB,CAAA;AACH,CAAC,CAAA;AAED,2HAA2H;AAC3H,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,KAA0B,EAAE,EAAE;IAC7D,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,SAAS,EAAE,GAAG,KAAK,CAAA;IAC1E,MAAM,WAAW,GAAG,OAAO,QAAQ,KAAK,QAAQ,CAAA;IAEhD,OAAO,CACL,KAAC,IAAI,CAAC,IAAI,IACR,GAAG,EAAE,GAAG,KACJ,SAAS,EACb,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,gBAAgB,EAAE,SAAS,CAAC,EAC9C,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,YAElC,WAAW,CAAC,CAAC,CAAC,yBAAO,QAAQ,GAAQ,CAAC,CAAC,CAAC,QAAQ,GACvC,CACb,CAAA;AACH,CAAC,CAAA;AAED,mEAAmE;AACnE,wDAAwD;AACxD,OAAO,CAAC,IAAI,GAAG,IAAI,CAAA;AACnB,OAAO,CAAC,OAAO,GAAG,OAAO,CAAA;AACzB,OAAO,CAAC,OAAO,GAAG,OAAO,CAAA;AACzB,iEAAiE;AACjE,sDAAsD;AACtD,mGAAmG;AACnG,OAAO,CAAC,gBAAgB,GAAG,gBAAgB,CAAA","sourcesContent":["\"use client\"\n\nimport clsx from \"clsx\"\nimport { Tooltip as RadixTooltip, Slot } from \"radix-ui\"\nimport { createContext, useContext, useRef, useState, type MouseEventHandler, type PointerEventHandler } from \"react\"\nimport { useTimeout } from \"usehooks-ts\"\nimport { useEscCloseStack } from \"../../hooks/useEscCloseStack\"\nimport { preventDefaultHandler } from \"../../lib/helpers\"\nimport { isFocusVisible, isHoverPointer, notePointerInput, trackInputModality } from \"../../lib/inputMode\"\nimport s from \"@plexui/styles/components/Tooltip/Tooltip.module.css\"\n\n// A tooltip opens on a focus that shows its ring only (isFocusVisible): it follows the last input of the page\nif (typeof document !== \"undefined\") trackInputModality(document)\n\nexport type TooltipProps = {\n children: React.ReactNode\n /**\n * The content of the tooltip. If `null`, the tooltip will not render.\n */\n content: React.ReactNode\n /**\n * Defines the `max-width` of the tooltip content. `\"none\"` creates a single-line, naturally sized tooltip.\n * @default 300\n */\n maxWidth?: number | \"none\"\n /**\n * Forces the tooltip to remain open or closed\n */\n forceOpen?: boolean\n /**\n * Delay of when the tooltip is shown from first interaction, in milliseconds.\n * @default 150\n */\n openDelay?: number\n /** Indicates that the tooltip has interactive content, and should remain open when hovered. */\n interactive?: boolean\n /**\n * Short, 1-3 word tooltips, stylized inversely from normal tooltips\n */\n compact?: boolean\n /**\n * Prevents the tooltip from closing when the trigger is clicked right after opening\n * @default false\n */\n preventUnintentionalClickToClose?: true\n /**\n * The preferred alignment against the trigger. May change when collisions occur.\n * @default center\n */\n align?: RadixTooltip.TooltipContentProps[\"align\"]\n /**\n * An offset in pixels from the \"start\" or \"end\" alignment options.\n * @default 0\n */\n alignOffset?: RadixTooltip.TooltipContentProps[\"alignOffset\"]\n /**\n * The preferred side of the trigger to render against when open. Will be reversed when collisions occur.\n * @default top\n */\n side?: RadixTooltip.TooltipContentProps[\"side\"]\n /**\n * The distance in pixels from the trigger.\n * @default 5\n */\n sideOffset?: RadixTooltip.TooltipContentProps[\"sideOffset\"]\n /**\n * Gutter sizing inside the tooltip content\n * @default md\n */\n gutterSize?: \"sm\" | \"md\" | \"lg\"\n /** Ref for the tooltip */\n ref?: React.Ref<HTMLDivElement | null>\n onPointerDown?: PointerEventHandler\n onClick?: MouseEventHandler\n /** Optional class name to apply to the tooltip content */\n contentClassName?: string\n}\n\nexport const Tooltip = (props: TooltipProps) => {\n const {\n ref: forwardedRef,\n children,\n content,\n forceOpen = content === null ? false : undefined,\n maxWidth = 300,\n openDelay = 150,\n interactive = false,\n compact = false,\n preventUnintentionalClickToClose,\n align,\n alignOffset = 0,\n side,\n sideOffset = 5,\n gutterSize = \"md\",\n contentClassName,\n onPointerDown,\n onClick,\n ...restProps\n } = props\n const [visible, setVisible] = useState<boolean>(false)\n const [temporarilyPreventClickToClose, setTemporarilyPreventClickToClose] =\n useState<boolean>(false)\n useTimeout(\n () => setTemporarilyPreventClickToClose(false),\n temporarilyPreventClickToClose ? 400 : null,\n )\n const open = forceOpen ?? visible\n\n const handleOpenChange = (nextState: boolean) => {\n // When forceOpen is passed, don't manage internal state\n if (typeof forceOpen === \"boolean\") return\n\n setVisible(nextState)\n\n if (preventUnintentionalClickToClose) {\n setTemporarilyPreventClickToClose(nextState)\n }\n }\n\n const maybePreventClickClose = (evt: React.MouseEvent) => {\n if (preventUnintentionalClickToClose && temporarilyPreventClickToClose) {\n // Prevents tooltip listeners from closing\n evt.preventDefault()\n evt.stopPropagation()\n }\n }\n\n return (\n <Root\n open={open}\n delayDuration={openDelay}\n onOpenChange={handleOpenChange}\n disableHoverableContent={!interactive}\n >\n <Trigger asChild>\n <Slot.Root\n {...restProps}\n ref={forwardedRef}\n onPointerDown={(evt) => {\n maybePreventClickClose(evt)\n onPointerDown?.(evt)\n }}\n onClick={(evt) => {\n maybePreventClickClose(evt)\n onClick?.(evt)\n }}\n >\n {children}\n </Slot.Root>\n </Trigger>\n <Content\n maxWidth={maxWidth}\n compact={compact}\n align={align}\n alignOffset={alignOffset}\n side={side}\n sideOffset={sideOffset}\n gutterSize={gutterSize}\n className={contentClassName}\n >\n {content}\n </Content>\n </Root>\n )\n}\n\nconst RecoverFocusContext = createContext<{\n recover: () => void\n state: \"instant-open\" | undefined\n} | undefined>(undefined)\n\nconst Root = ({\n children,\n open,\n onOpenChange,\n ...restProps\n}: RadixTooltip.TooltipProps & {\n open: boolean\n onOpenChange: (nextState: boolean) => void\n}) => {\n const [recoveredFocus, setRecoveredFocus] = useState(false)\n const handleOpenChange = (nextState: boolean) => {\n setRecoveredFocus(false)\n onOpenChange(nextState)\n }\n useEscCloseStack(open, () => handleOpenChange(false))\n\n return (\n <RadixTooltip.Provider>\n <RadixTooltip.Root open={open} onOpenChange={handleOpenChange} {...restProps}>\n <RecoverFocusContext.Provider value={{\n recover: () => {\n if (open) return\n // Match a keyboard open even if Radix retained its cancelled\n // pointer flag and a previous hover's delayed-open state.\n document.dispatchEvent(new CustomEvent(\"tooltip.open\"))\n setRecoveredFocus(true)\n onOpenChange(true)\n },\n state: open && recoveredFocus ? \"instant-open\" : undefined,\n }}>\n {children}\n </RecoverFocusContext.Provider>\n </RadixTooltip.Root>\n </RadixTooltip.Provider>\n )\n}\n\ntype TooltipContentProps = RadixTooltip.TooltipContentProps & {\n children: React.ReactNode\n /**\n * Defines the `max-width` of the tooltip content. `\"none\"` creates a single-line, naturally sized tooltip.\n * @default 300\n */\n maxWidth?: number | \"none\"\n /**\n * Short, 1-3 word tooltips, stylized inversely from normal tooltips\n */\n compact?: boolean\n /**\n * Gutter sizing inside the tooltip content\n * @default md\n */\n gutterSize?: \"sm\" | \"md\" | \"lg\"\n /** Determines if the tooltip content should respond to click events */\n clickable?: boolean\n}\n\nexport const Content = ({\n children,\n maxWidth = 300,\n compact = false,\n clickable = undefined,\n alignOffset = 0,\n sideOffset = 5,\n gutterSize = \"md\",\n className,\n style,\n ...restProps\n}: TooltipContentProps) => {\n const focusRecovery = useContext(RecoverFocusContext)\n return (\n <RadixTooltip.Portal>\n <RadixTooltip.Content\n {...restProps}\n {...(focusRecovery?.state && { \"data-state\": focusRecovery.state })}\n className={clsx(s.Tooltip, className)}\n data-compact={compact}\n data-clickable={clickable}\n data-gutter-size={gutterSize}\n alignOffset={alignOffset}\n sideOffset={sideOffset}\n collisionPadding={15}\n hideWhenDetached\n style={{\n ...style,\n maxWidth,\n }}\n onEscapeKeyDown={preventDefaultHandler}\n >\n {children}\n </RadixTooltip.Content>\n </RadixTooltip.Portal>\n )\n}\n\ntype TooltipTriggerProps = RadixTooltip.TooltipTriggerProps & {\n /**\n * Determines if `tabindex=0` is applied to the trigger\n * @default true\n */\n focusable?: boolean\n /**\n * Optional class name to apply to the trigger\n */\n className?: string\n /** Ref for the trigger */\n ref?: React.Ref<HTMLSpanElement | null>\n}\n\nexport const Trigger = ({\n children,\n asChild = true,\n onPointerMove,\n onPointerLeave,\n onPointerDown,\n onPointerCancel,\n onFocus,\n onBlur,\n ...restProps\n}: RadixTooltip.TooltipTriggerProps) => {\n const focusRecovery = useContext(RecoverFocusContext)\n const cancelledPointer = useRef(false)\n\n return (\n <RadixTooltip.Trigger\n asChild={asChild}\n {...restProps}\n {...(focusRecovery?.state && { \"data-state\": focusRecovery.state })}\n onPointerDown={(event) => {\n cancelledPointer.current = false\n // A trigger in another document (an iframe) tracks that document's input\n notePointerInput(event.currentTarget.ownerDocument)\n onPointerDown?.(event)\n }}\n onPointerCancel={(event) => {\n // Radix only clears its pointer-down flag on pointerup. A cancelled\n // touch gesture has no pointerup, so recover the next keyboard focus.\n cancelledPointer.current = true\n onPointerCancel?.(event)\n }}\n onFocus={(event) => {\n trackInputModality(event.currentTarget.ownerDocument)\n onFocus?.(event)\n if (event.defaultPrevented) return\n if (event.currentTarget.hasAttribute(\"data-menu-pointer-focus\")) {\n event.preventDefault()\n } else if (!isFocusVisible(event.currentTarget)) {\n // A focus after a mouse press that a script moved here (a dialog closed with the mouse returns focus to its\n // trigger) shows no ring, so no tooltip: it stayed open until the focus left\n event.preventDefault()\n } else if (cancelledPointer.current) {\n event.preventDefault()\n focusRecovery?.recover()\n }\n }}\n onBlur={(event) => {\n onBlur?.(event)\n // Clipboard fallback borrows focus synchronously, then restores it.\n if (event.relatedTarget instanceof Element && event.relatedTarget.hasAttribute(\"data-plex-clipboard-fallback\")) {\n event.preventDefault()\n }\n }}\n onPointerMove={(event) => {\n trackInputModality(event.currentTarget.ownerDocument)\n onPointerMove?.(event)\n if (!isHoverPointer(event)) event.preventDefault()\n }}\n onPointerLeave={(event) => {\n onPointerLeave?.(event)\n if (!isHoverPointer(event)) event.preventDefault()\n }}\n >\n {children}\n </RadixTooltip.Trigger>\n )\n}\n\n// Optional wrapper for tooltip trigger, if the visual content isn't already accessible or with built-in visual indications\nexport const TriggerDecorator = (props: TooltipTriggerProps) => {\n const { children, className, focusable = true, ref, ...restProps } = props\n const isPlainText = typeof children === \"string\"\n\n return (\n <Slot.Root\n ref={ref}\n {...restProps}\n className={clsx(s.TriggerDecorator, className)}\n tabIndex={focusable ? 0 : undefined}\n >\n {isPlainText ? <span>{children}</span> : children}\n </Slot.Root>\n )\n}\n\n// Building advanced tooltip behaviors can be achieved by using the\n// composed components, and wiring up bespoke behaviors.\nTooltip.Root = Root\nTooltip.Content = Content\nTooltip.Trigger = Trigger\n// Basic <Tooltip> usage can be done with the component directly,\n// without requiring controlled component composition.\n// <TriggerDecorator> is a helper component that creates an accessible trigger with a visual effect\nTooltip.TriggerDecorator = TriggerDecorator\n"]}
@@ -6,4 +6,44 @@ export const isHoverPointer = (event) => {
6
6
  const doc = target instanceof Document ? target : target instanceof Node ? target.ownerDocument : null;
7
7
  return doc?.documentElement.dataset.input !== "touch";
8
8
  };
9
+ // The last input of each document: a pointer press or a key. A focus that follows a pointer press is one the press
10
+ // gave or a script moved after it (a dialog closed with the mouse returns focus to its trigger), and no focus ring
11
+ // shows for it; a key, Tab or an arrow, shows one.
12
+ const lastInput = new WeakMap();
13
+ const tracked = new WeakSet();
14
+ /** Starts tracking the last input of a document (once per document). */
15
+ export const trackInputModality = (doc) => {
16
+ if (!doc || tracked.has(doc))
17
+ return;
18
+ tracked.add(doc);
19
+ doc.addEventListener("pointerdown", () => lastInput.set(doc, "pointer"), { capture: true, passive: true });
20
+ doc.addEventListener("mousedown", () => lastInput.set(doc, "pointer"), { capture: true, passive: true });
21
+ // Any key counts, with a modifier too: a screen reader moves focus with its own keys (Control-Option-arrows), and
22
+ // a tooltip it misses is worse than one more after a shortcut
23
+ doc.addEventListener("keydown", () => lastInput.set(doc, "keyboard"), { capture: true, passive: true });
24
+ };
25
+ /**
26
+ * Notes a pointer press from the element's own handler: a document that starts being tracked during that press (the
27
+ * first touch on a trigger in an iframe) has already let it pass its capture phase.
28
+ */
29
+ export const notePointerInput = (doc) => {
30
+ if (!doc)
31
+ return;
32
+ trackInputModality(doc);
33
+ lastInput.set(doc, "pointer");
34
+ };
35
+ /**
36
+ * Whether a focus shows its ring, as the browser's :focus-visible: a focus after a key or with no input yet does, one
37
+ * a pointer press gave or a script moved after it does not. A tooltip opens on the first only.
38
+ */
39
+ export const isFocusVisible = (element) => {
40
+ try {
41
+ if (element.matches(":focus-visible"))
42
+ return true;
43
+ }
44
+ catch {
45
+ // A browser without the selector leaves it to the last input
46
+ }
47
+ return lastInput.get(element.ownerDocument) !== "pointer";
48
+ };
9
49
  //# sourceMappingURL=inputMode.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"inputMode.js","sourceRoot":"","sources":["../../../src/lib/inputMode.ts"],"names":[],"mappings":"AAKA,0FAA0F;AAC1F,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAwB,EAAW,EAAE;IAClE,IAAI,KAAK,CAAC,WAAW,KAAK,OAAO;QAAE,OAAO,KAAK,CAAA;IAC/C,MAAM,MAAM,GAAG,KAAK,CAAC,aAAa,CAAA;IAClC,MAAM,GAAG,GAAG,MAAM,YAAY,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,YAAY,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAA;IACtG,OAAO,GAAG,EAAE,eAAe,CAAC,OAAO,CAAC,KAAK,KAAK,OAAO,CAAA;AACvD,CAAC,CAAA","sourcesContent":["type HoverPointerEvent = {\n pointerType: string\n currentTarget: EventTarget | null\n}\n\n/** Pointer hover is unavailable to touch input and explicitly touch-rendered surfaces. */\nexport const isHoverPointer = (event: HoverPointerEvent): boolean => {\n if (event.pointerType === \"touch\") return false\n const target = event.currentTarget\n const doc = target instanceof Document ? target : target instanceof Node ? target.ownerDocument : null\n return doc?.documentElement.dataset.input !== \"touch\"\n}\n"]}
1
+ {"version":3,"file":"inputMode.js","sourceRoot":"","sources":["../../../src/lib/inputMode.ts"],"names":[],"mappings":"AAKA,0FAA0F;AAC1F,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAwB,EAAW,EAAE;IAClE,IAAI,KAAK,CAAC,WAAW,KAAK,OAAO;QAAE,OAAO,KAAK,CAAA;IAC/C,MAAM,MAAM,GAAG,KAAK,CAAC,aAAa,CAAA;IAClC,MAAM,GAAG,GAAG,MAAM,YAAY,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,YAAY,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAA;IACtG,OAAO,GAAG,EAAE,eAAe,CAAC,OAAO,CAAC,KAAK,KAAK,OAAO,CAAA;AACvD,CAAC,CAAA;AAED,mHAAmH;AACnH,mHAAmH;AACnH,mDAAmD;AACnD,MAAM,SAAS,GAAG,IAAI,OAAO,EAAoC,CAAA;AACjE,MAAM,OAAO,GAAG,IAAI,OAAO,EAAY,CAAA;AAEvC,wEAAwE;AACxE,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,GAAgC,EAAQ,EAAE;IAC3E,IAAI,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;QAAE,OAAM;IACpC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAChB,GAAG,CAAC,gBAAgB,CAAC,aAAa,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,SAAS,CAAC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAA;IAC1G,GAAG,CAAC,gBAAgB,CAAC,WAAW,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,SAAS,CAAC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAA;IACxG,kHAAkH;IAClH,8DAA8D;IAC9D,GAAG,CAAC,gBAAgB,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,UAAU,CAAC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAA;AACzG,CAAC,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,GAAgC,EAAQ,EAAE;IACzE,IAAI,CAAC,GAAG;QAAE,OAAM;IAChB,kBAAkB,CAAC,GAAG,CAAC,CAAA;IACvB,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,SAAS,CAAC,CAAA;AAC/B,CAAC,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,OAAgB,EAAW,EAAE;IAC1D,IAAI,CAAC;QACH,IAAI,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAC;YAAE,OAAO,IAAI,CAAA;IACpD,CAAC;IAAC,MAAM,CAAC;QACP,6DAA6D;IAC/D,CAAC;IACD,OAAO,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC,KAAK,SAAS,CAAA;AAC3D,CAAC,CAAA","sourcesContent":["type HoverPointerEvent = {\n pointerType: string\n currentTarget: EventTarget | null\n}\n\n/** Pointer hover is unavailable to touch input and explicitly touch-rendered surfaces. */\nexport const isHoverPointer = (event: HoverPointerEvent): boolean => {\n if (event.pointerType === \"touch\") return false\n const target = event.currentTarget\n const doc = target instanceof Document ? target : target instanceof Node ? target.ownerDocument : null\n return doc?.documentElement.dataset.input !== \"touch\"\n}\n\n// The last input of each document: a pointer press or a key. A focus that follows a pointer press is one the press\n// gave or a script moved after it (a dialog closed with the mouse returns focus to its trigger), and no focus ring\n// shows for it; a key, Tab or an arrow, shows one.\nconst lastInput = new WeakMap<Document, \"pointer\" | \"keyboard\">()\nconst tracked = new WeakSet<Document>()\n\n/** Starts tracking the last input of a document (once per document). */\nexport const trackInputModality = (doc: Document | null | undefined): void => {\n if (!doc || tracked.has(doc)) return\n tracked.add(doc)\n doc.addEventListener(\"pointerdown\", () => lastInput.set(doc, \"pointer\"), { capture: true, passive: true })\n doc.addEventListener(\"mousedown\", () => lastInput.set(doc, \"pointer\"), { capture: true, passive: true })\n // Any key counts, with a modifier too: a screen reader moves focus with its own keys (Control-Option-arrows), and\n // a tooltip it misses is worse than one more after a shortcut\n doc.addEventListener(\"keydown\", () => lastInput.set(doc, \"keyboard\"), { capture: true, passive: true })\n}\n\n/**\n * Notes a pointer press from the element's own handler: a document that starts being tracked during that press (the\n * first touch on a trigger in an iframe) has already let it pass its capture phase.\n */\nexport const notePointerInput = (doc: Document | null | undefined): void => {\n if (!doc) return\n trackInputModality(doc)\n lastInput.set(doc, \"pointer\")\n}\n\n/**\n * Whether a focus shows its ring, as the browser's :focus-visible: a focus after a key or with no input yet does, one\n * a pointer press gave or a script moved after it does not. A tooltip opens on the first only.\n */\nexport const isFocusVisible = (element: Element): boolean => {\n try {\n if (element.matches(\":focus-visible\")) return true\n } catch {\n // A browser without the selector leaves it to the last input\n }\n return lastInput.get(element.ownerDocument) !== \"pointer\"\n}\n"]}
@@ -4,4 +4,16 @@ type HoverPointerEvent = {
4
4
  };
5
5
  /** Pointer hover is unavailable to touch input and explicitly touch-rendered surfaces. */
6
6
  export declare const isHoverPointer: (event: HoverPointerEvent) => boolean;
7
+ /** Starts tracking the last input of a document (once per document). */
8
+ export declare const trackInputModality: (doc: Document | null | undefined) => void;
9
+ /**
10
+ * Notes a pointer press from the element's own handler: a document that starts being tracked during that press (the
11
+ * first touch on a trigger in an iframe) has already let it pass its capture phase.
12
+ */
13
+ export declare const notePointerInput: (doc: Document | null | undefined) => void;
14
+ /**
15
+ * Whether a focus shows its ring, as the browser's :focus-visible: a focus after a key or with no input yet does, one
16
+ * a pointer press gave or a script moved after it does not. A tooltip opens on the first only.
17
+ */
18
+ export declare const isFocusVisible: (element: Element) => boolean;
7
19
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plexui/ui",
3
- "version": "0.59.0",
3
+ "version": "0.59.1",
4
4
  "description": "Modern design system for building high-quality applications",
5
5
  "type": "module",
6
6
  "license": "MIT",