Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

feat(selector): disabledMessage tooltip on disabled Selector/MultiSelector (proposal for #3347) (#3356)

David Petersenb753238ff9a0
diff --git a/.changeset/selector-disabled-message.md b/.changeset/selector-disabled-message.md
new file mode 100644
index 0000000000..32e7d43e85
--- /dev/null
+++ b/.changeset/selector-disabled-message.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[feat] Selector/MultiSelector: disabledMessage prop shows a tooltip explaining the disabled state (#3347)
+@thedjpetersen
diff --git a/apps/storybook/stories/MultiSelector.stories.tsx b/apps/storybook/stories/MultiSelector.stories.tsx
index 1ed050004b..31129b2e26 100644
--- a/apps/storybook/stories/MultiSelector.stories.tsx
+++ b/apps/storybook/stories/MultiSelector.stories.tsx
@@ -29,6 +29,7 @@ const meta: Meta<typeof MultiSelector> = {
options: ['count', 'labels', 'badges'],
},
isDisabled: {control: 'boolean'},
+ disabledMessage: {control: 'text'},
isOptional: {control: 'boolean'},
isRequired: {control: 'boolean'},
hasSelectAll: {control: 'boolean'},
@@ -207,6 +208,29 @@ export const DisabledItems: Story = {
decorators: [Story => <Story />],
};
+// Disabled with an explanation tooltip. Hover or keyboard-focus the trigger to
+// see why it's disabled — the reason is announced to assistive tech via
+// aria-describedby, and the trigger stays focusable (activation is still
+// blocked). Use disabledMessage instead of wrapping a disabled MultiSelector in
+// Tooltip: disabled controls swallow the pointer events a Tooltip wrapper needs.
+export const DisabledWithMessage: Story = {
+ render: () => {
+ const [value, setValue] = useState<string[]>([]);
+ return (
+ <MultiSelector
+ label="Columns"
+ options={['Name', 'Email', 'Role', 'Status', 'Created']}
+ value={value}
+ onChange={setValue}
+ isDisabled
+ disabledMessage="Select a table before choosing columns"
+ placeholder="Select columns..."
+ />
+ );
+ },
+ decorators: [Story => <Story />],
+};
+
// Status variants
export const Status: Story = {
render: () => {
diff --git a/apps/storybook/stories/Selector.stories.tsx b/apps/storybook/stories/Selector.stories.tsx
index 192c3f3575..574d423887 100644
--- a/apps/storybook/stories/Selector.stories.tsx
+++ b/apps/storybook/stories/Selector.stories.tsx
@@ -60,6 +60,11 @@ const meta: Meta<typeof Selector> = {
control: 'boolean',
description: 'Whether the selector is disabled',
},
+ disabledMessage: {
+ control: 'text',
+ description:
+ 'Explains why the selector is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the trigger focusable via aria-disabled (activation stays blocked). Use this instead of wrapping a disabled Selector in Tooltip.',
+ },
isOptional: {
control: 'boolean',
description: 'Whether the field is optional',
@@ -431,6 +436,21 @@ export const Disabled: Story = {
},
};
+// Disabled with an explanation tooltip. Hover or keyboard-focus the trigger to
+// see why it's disabled — the reason is announced to assistive tech via
+// aria-describedby, and the trigger stays focusable (activation is still
+// blocked). Use disabledMessage instead of wrapping a disabled Selector in
+// Tooltip: disabled controls swallow the pointer events a Tooltip wrapper needs.
+export const DisabledWithMessage: Story = {
+ args: {
+ label: 'Owner',
+ options: ['Alice', 'Bob', 'Carol'],
+ isDisabled: true,
+ disabledMessage: 'You need the Editor role to change this',
+ placeholder: 'Select an owner...',
+ },
+};
+
// Pre-selected
export const PreSelected: Story = {
render: args => {
diff --git a/packages/core/src/MultiSelector/MultiSelector.doc.mjs b/packages/core/src/MultiSelector/MultiSelector.doc.mjs
index e8dc6957b3..47dc3e5291 100644
--- a/packages/core/src/MultiSelector/MultiSelector.doc.mjs
+++ b/packages/core/src/MultiSelector/MultiSelector.doc.mjs
@@ -112,6 +112,12 @@ export const docs = {
type: 'boolean',
description: 'Disables the selector.',
},
+ {
+ name: 'disabledMessage',
+ type: 'string',
+ description:
+ 'Explains why the selector is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the trigger focusable via aria-disabled (activation stays blocked). Use this instead of wrapping a disabled MultiSelector in Tooltip — disabled controls swallow the hover events an external Tooltip needs.',
+ },
{
name: 'isLabelHidden',
type: 'boolean',
@@ -189,6 +195,11 @@ export const docs = {
guidance: false,
description: 'Show more than ~20 options without enabling search.',
},
+ {
+ guidance: false,
+ description:
+ 'Wrap a disabled MultiSelector in Tooltip to explain why it is disabled; disabled triggers swallow the hover events the wrapper needs. Use the disabledMessage prop instead.',
+ },
],
},
};
@@ -217,6 +228,8 @@ export const docsZh = {
hasSearch: '是否显示用于过滤选项的搜索输入。',
searchPlaceholder: '搜索输入的占位文本。',
isDisabled: '禁用选择器。',
+ disabledMessage:
+ '解释选择器被禁用的原因。与 isDisabled 一起使用时,悬停/键盘聚焦时显示工具提示,并通过 aria-disabled 保持触发器可聚焦(仍无法激活)。请使用此属性,而不是用 Tooltip 包裹被禁用的选择器。',
isLabelHidden: '视觉上隐藏标签同时保持其可访问性。',
description: '标签下方显示的辅助文本。',
isOptional: '将字段标记为可选。',
@@ -261,6 +274,11 @@ export const docsZh = {
guidance: false,
description: 'Show more than ~20 options without enabling search.',
},
+ {
+ guidance: false,
+ description:
+ 'Wrap a disabled MultiSelector in Tooltip to explain why it is disabled; disabled triggers swallow the hover events the wrapper needs. Use the disabledMessage prop instead.',
+ },
],
},
};
@@ -301,6 +319,11 @@ export const docsDense = {
guidance: false,
description: 'Show more than ~20 options without enabling search.',
},
+ {
+ guidance: false,
+ description:
+ 'Wrap a disabled MultiSelector in Tooltip to explain why it is disabled; disabled triggers swallow the hover events the wrapper needs. Use the disabledMessage prop instead.',
+ },
],
},
components: [
@@ -324,6 +347,8 @@ export const docsDense = {
hasSearch: 'show search input',
searchPlaceholder: 'search placeholder',
isDisabled: 'disables selector',
+ disabledMessage:
+ 'why disabled; w/ isDisabled shows tooltip on hover/focus, trigger stays focusable via aria-disabled; use instead of Tooltip wrapper',
isLabelHidden: 'visually hides label',
description: 'helper text below label',
isOptional: 'marks optional',
diff --git a/packages/core/src/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsx
index d202302548..4252246e2d 100644
--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx
+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx
@@ -10,7 +10,7 @@
*/
import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';
-import {render, screen, waitFor} from '@testing-library/react';
+import {render, screen, fireEvent, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {MultiSelector} from './MultiSelector';
import {__resetLiveRegionsForTest} from '../hooks/useAnnounce';
@@ -893,4 +893,153 @@ describe('MultiSelector', () => {
});
});
});
+
+ describe('disabledMessage', () => {
+ it('shows the reason tooltip on hover when disabled with a reason', async () => {
+ render(
+ <MultiSelector
+ label="Fruit"
+ options={defaultOptions}
+ value={[]}
+ onChange={() => {}}
+ isDisabled
+ disabledMessage="Select a table first"
+ data-testid="fruit-multi-selector"
+ />,
+ );
+
+ const container = screen.getByTestId('fruit-multi-selector');
+ const tooltip = screen.getByRole('tooltip', h);
+ expect(tooltip).toHaveTextContent('Select a table first');
+
+ fireEvent.mouseEnter(container);
+ await waitFor(() => {
+ expect(tooltip).toHaveAttribute('popover-open');
+ });
+
+ fireEvent.mouseLeave(container);
+ await waitFor(() => {
+ expect(tooltip).not.toHaveAttribute('popover-open');
+ });
+ });
+
+ it('shows the reason tooltip on keyboard focus', async () => {
+ const user = userEvent.setup();
+ render(
+ <MultiSelector
+ label="Fruit"
+ options={defaultOptions}
+ value={[]}
+ onChange={() => {}}
+ isDisabled
+ disabledMessage="Select a table first"
+ />,
+ );
+
+ const tooltip = screen.getByRole('tooltip', h);
+ await user.tab();
+ expect(screen.getByRole('combobox')).toHaveFocus();
+ await waitFor(() => {
+ expect(tooltip).toHaveAttribute('popover-open');
+ });
+ });
+
+ it('does not render a tooltip when not disabled', () => {
+ render(
+ <MultiSelector
+ label="Fruit"
+ options={defaultOptions}
+ value={[]}
+ onChange={() => {}}
+ disabledMessage="Select a table first"
+ />,
+ );
+ expect(screen.queryByRole('tooltip', h)).not.toBeInTheDocument();
+ });
+
+ it('does not render a tooltip when disabled without a reason', () => {
+ render(
+ <MultiSelector
+ label="Fruit"
+ options={defaultOptions}
+ value={[]}
+ onChange={() => {}}
+ isDisabled
+ />,
+ );
+ expect(screen.queryByRole('tooltip', h)).not.toBeInTheDocument();
+ });
+
+ it('keeps the trigger focusable via aria-disabled when a reason is provided', () => {
+ render(
+ <MultiSelector
+ label="Fruit"
+ options={defaultOptions}
+ value={[]}
+ onChange={() => {}}
+ isDisabled
+ disabledMessage="Select a table first"
+ />,
+ );
+ const trigger = screen.getByRole('combobox');
+ expect(trigger).not.toBeDisabled();
+ expect(trigger).toHaveAttribute('aria-disabled', 'true');
+ expect(trigger).toHaveAttribute('tabIndex', '0');
+ });
+
+ it('links the reason tooltip from the trigger via aria-describedby', () => {
+ render(
+ <MultiSelector
+ label="Fruit"
+ options={defaultOptions}
+ value={[]}
+ onChange={() => {}}
+ isDisabled
+ disabledMessage="Select a table first"
+ />,
+ );
+ const trigger = screen.getByRole('combobox');
+ const tooltip = screen.getByRole('tooltip', h);
+ expect(trigger.getAttribute('aria-describedby')).toContain(tooltip.id);
+ });
+
+ it('blocks activation while focusable-disabled', async () => {
+ const user = userEvent.setup();
+ const onChange = vi.fn();
+ render(
+ <MultiSelector
+ label="Fruit"
+ options={defaultOptions}
+ value={[]}
+ onChange={onChange}
+ isDisabled
+ disabledMessage="Select a table first"
+ />,
+ );
+
+ const trigger = screen.getByRole('combobox');
+ await user.click(trigger);
+ expect(trigger).toHaveAttribute('aria-expanded', 'false');
+
+ await user.keyboard('{Enter}');
+ await user.keyboard('{ArrowDown}');
+ expect(trigger).toHaveAttribute('aria-expanded', 'false');
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it('remains non-focusable when disabled without a reason', () => {
+ render(
+ <MultiSelector
+ label="Fruit"
+ options={defaultOptions}
+ value={[]}
+ onChange={() => {}}
+ isDisabled
+ />,
+ );
+ const trigger = screen.getByRole('combobox');
+ expect(trigger).toBeDisabled();
+ expect(trigger).toHaveAttribute('tabIndex', '-1');
+ });
+ });
});
diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsx
index 0f27fd5696..8cd937cfa1 100644
--- a/packages/core/src/MultiSelector/MultiSelector.tsx
+++ b/packages/core/src/MultiSelector/MultiSelector.tsx
@@ -4,7 +4,7 @@
/**
* @file MultiSelector.tsx
- * @input Uses React, StyleX, usePopover, CheckboxInput, Field, Badge, Icon
+ * @input Uses React, StyleX, usePopover, useTooltip, CheckboxInput, Field, Badge, Icon
* @output Exports MultiSelector component
* @position Core implementation; consumed by index.ts
*
@@ -27,6 +27,7 @@ import React, {
} from 'react';
import * as stylex from '@stylexjs/stylex';
import {usePopover} from '../Popover/usePopover';
+import {useTooltip} from '../Tooltip';
import {Icon, renderIconSlot, type IconType} from '../Icon';
import type {IconName} from '../Icon';
import {
@@ -398,6 +399,30 @@ export interface MultiSelectorProps<
*/
isDisabled?: boolean;
+ /**
+ * Explains why the selector is disabled. When set together with
+ * `isDisabled`, the selector shows a tooltip with this text on hover and
+ * keyboard focus, and the trigger stays focusable (via `aria-disabled`)
+ * so the reason is discoverable by keyboard and assistive technology.
+ * Activation stays blocked.
+ *
+ * Use this instead of wrapping a disabled selector in `Tooltip` — disabled
+ * controls don't emit the pointer events an external tooltip needs.
+ *
+ * @example
+ * ```
+ * <MultiSelector
+ * label="Columns"
+ * options={columns}
+ * value={selected}
+ * onChange={setSelected}
+ * isDisabled
+ * disabledMessage="Select a table first"
+ * />
+ * ```
+ */
+ disabledMessage?: string;
+
/**
* The options to display in the selector.
* Can be strings, objects, dividers, or sections.
@@ -546,6 +571,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
isOptional = false,
isRequired = false,
isDisabled = false,
+ disabledMessage,
options,
value,
onChange,
@@ -593,11 +619,26 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
const [optimisticValue, setOptimisticValue] = useOptimistic(value);
const isBusy = isLoading || optimisticValue !== value;
+ // Disabled-reason tooltip. Disabled controls swallow pointer events, so the
+ // tooltip listeners attach to the trigger container (which already exists)
+ // and the trigger button stays perceivable via aria-disabled instead of the
+ // disabled attribute. Activation is blocked by the isDisabled guards in
+ // useMultiCombobox (onTriggerClick / onKeyDown).
+ const showsDisabledMessage = isDisabled && !!disabledMessage;
+ const disabledMessageTooltip = useTooltip({
+ placement: 'above',
+ // The container div is not naturally focusable; focusin bubbles up from
+ // the trigger button, so always attach focus listeners.
+ focusTrigger: 'always',
+ isEnabled: showsDisabledMessage,
+ });
+
// Build aria-describedby
const ariaDescribedBy =
[
description ? descriptionId : null,
status?.message ? statusMessageId : null,
+ showsDisabledMessage ? disabledMessageTooltip.describedBy : null,
]
.filter(Boolean)
.join(' ') || undefined;
@@ -1190,6 +1231,10 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
<div
ref={el => {
popover.triggerRef(el);
+ // Anchor + hover/focus listeners for the disabled-message tooltip.
+ // Handlers are gated internally by isEnabled, and anchor names
+ // compose, so attaching unconditionally is safe.
+ disabledMessageTooltip.ref(el);
}}
onClick={onTriggerClick}
data-testid={testId}
@@ -1230,9 +1275,13 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
aria-required={isRequired ? 'true' : undefined}
aria-invalid={status?.type === 'error' ? 'true' : undefined}
aria-busy={isBusy || undefined}
- disabled={isDisabled}
+ // With a disabledMessage the trigger keeps focusability via
+ // aria-disabled so the reason is focus-discoverable; activation is
+ // still blocked by the isDisabled guards in useMultiCombobox.
+ disabled={isDisabled && !showsDisabledMessage}
+ aria-disabled={showsDisabledMessage ? 'true' : undefined}
onKeyDown={onKeyDown}
- tabIndex={isDisabled ? -1 : 0}
+ tabIndex={isDisabled && !showsDisabledMessage ? -1 : 0}
{...stylex.props(styles.trigger)}>
<span {...stylex.props(styles.triggerContent)}>
{renderTriggerContent()}
@@ -1283,6 +1332,9 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
xstyle: styles.popover,
},
)}
+
+ {showsDisabledMessage &&
+ disabledMessageTooltip.renderTooltip(disabledMessage)}
</Field>
);
}
diff --git a/packages/core/src/Selector/Selector.doc.mjs b/packages/core/src/Selector/Selector.doc.mjs
index 356e18c757..87d920b104 100644
--- a/packages/core/src/Selector/Selector.doc.mjs
+++ b/packages/core/src/Selector/Selector.doc.mjs
@@ -86,6 +86,12 @@ export const docs = {
description: 'Disables the selector.',
default: 'false',
},
+ {
+ name: 'disabledMessage',
+ type: 'string',
+ description:
+ 'Explains why the selector is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the trigger focusable via aria-disabled (activation stays blocked). Use this instead of wrapping a disabled Selector in Tooltip — disabled controls swallow the hover events an external Tooltip needs.',
+ },
{
name: 'isLabelHidden',
type: 'boolean',
@@ -176,6 +182,11 @@ export const docs = {
description:
'Put more than ~20 options without sections; consider Typeahead for large lists.',
},
+ {
+ guidance: false,
+ description:
+ 'Wrap a disabled Selector in Tooltip to explain why it is disabled; disabled triggers swallow the hover events the wrapper needs. Use the disabledMessage prop instead.',
+ },
],
anatomy: [
{
@@ -263,6 +274,11 @@ export const docsZh = {
description:
'Put more than ~20 options without sections; consider Typeahead for large lists.',
},
+ {
+ guidance: false,
+ description:
+ 'Wrap a disabled Selector in Tooltip to explain why it is disabled; disabled triggers swallow the hover events the wrapper needs. Use the disabledMessage prop instead.',
+ },
],
anatomy: [
{
@@ -350,6 +366,11 @@ export const docsDense = {
description:
'Put more than ~20 options without sections; consider Typeahead for large lists.',
},
+ {
+ guidance: false,
+ description:
+ 'Wrap a disabled Selector in Tooltip to explain why it is disabled; disabled triggers swallow the hover events the wrapper needs. Use the disabledMessage prop instead.',
+ },
],
anatomy: [
{
diff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsx
index 329505f8f8..d75c95f7f2 100644
--- a/packages/core/src/Selector/Selector.test.tsx
+++ b/packages/core/src/Selector/Selector.test.tsx
@@ -10,7 +10,7 @@
*/
import {describe, it, expect, vi, beforeEach} from 'vitest';
-import {render, screen, waitFor} from '@testing-library/react';
+import {render, screen, fireEvent, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {Selector} from './Selector';
import {SelectorOption} from './SelectorOption';
@@ -652,4 +652,126 @@ describe('Selector', () => {
}
});
});
+
+ describe('disabledMessage', () => {
+ it('shows the reason tooltip on hover when disabled with a reason', async () => {
+ render(
+ <Selector
+ label="Fruit"
+ options={OPTIONS}
+ isDisabled
+ disabledMessage="You need the Editor role"
+ data-testid="fruit-selector"
+ />,
+ );
+
+ const container = screen.getByTestId('fruit-selector');
+ const tooltip = screen.getByRole('tooltip', h);
+ expect(tooltip).toHaveTextContent('You need the Editor role');
+
+ fireEvent.mouseEnter(container);
+ await waitFor(() => {
+ expect(tooltip).toHaveAttribute('popover-open');
+ });
+
+ fireEvent.mouseLeave(container);
+ await waitFor(() => {
+ expect(tooltip).not.toHaveAttribute('popover-open');
+ });
+ });
+
+ it('shows the reason tooltip on keyboard focus', async () => {
+ const user = userEvent.setup();
+ render(
+ <Selector
+ label="Fruit"
+ options={OPTIONS}
+ isDisabled
+ disabledMessage="You need the Editor role"
+ />,
+ );
+
+ const tooltip = screen.getByRole('tooltip', h);
+ await user.tab();
+ expect(screen.getByRole('combobox')).toHaveFocus();
+ await waitFor(() => {
+ expect(tooltip).toHaveAttribute('popover-open');
+ });
+ });
+
+ it('does not render a tooltip when not disabled', () => {
+ render(
+ <Selector
+ label="Fruit"
+ options={OPTIONS}
+ disabledMessage="You need the Editor role"
+ />,
+ );
+ expect(screen.queryByRole('tooltip', h)).not.toBeInTheDocument();
+ });
+
+ it('does not render a tooltip when disabled without a reason', () => {
+ render(<Selector label="Fruit" options={OPTIONS} isDisabled />);
+ expect(screen.queryByRole('tooltip', h)).not.toBeInTheDocument();
+ });
+
+ it('keeps the trigger focusable via aria-disabled when a reason is provided', () => {
+ render(
+ <Selector
+ label="Fruit"
+ options={OPTIONS}
+ isDisabled
+ disabledMessage="You need the Editor role"
+ />,
+ );
+ const trigger = screen.getByRole('combobox');
+ expect(trigger).not.toBeDisabled();
+ expect(trigger).toHaveAttribute('aria-disabled', 'true');
+ expect(trigger).toHaveAttribute('tabIndex', '0');
+ });
+
+ it('links the reason tooltip from the trigger via aria-describedby', () => {
+ render(
+ <Selector
+ label="Fruit"
+ options={OPTIONS}
+ isDisabled
+ disabledMessage="You need the Editor role"
+ />,
+ );
+ const trigger = screen.getByRole('combobox');
+ const tooltip = screen.getByRole('tooltip', h);
+ expect(trigger.getAttribute('aria-describedby')).toContain(tooltip.id);
+ });
+
+ it('blocks activation while focusable-disabled', async () => {
+ const user = userEvent.setup();
+ const onChange = vi.fn();
+ render(
+ <Selector
+ label="Fruit"
+ options={OPTIONS}
+ onChange={onChange}
+ isDisabled
+ disabledMessage="You need the Editor role"
+ />,
+ );
+
+ const trigger = screen.getByRole('combobox');
+ await user.click(trigger);
+ expect(trigger).toHaveAttribute('aria-expanded', 'false');
+
+ await user.keyboard('{Enter}');
+ await user.keyboard('{ArrowDown}');
+ expect(trigger).toHaveAttribute('aria-expanded', 'false');
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it('remains non-focusable when disabled without a reason', () => {
+ render(<Selector label="Fruit" options={OPTIONS} isDisabled />);
+ const trigger = screen.getByRole('combobox');
+ expect(trigger).toBeDisabled();
+ expect(trigger).toHaveAttribute('tabIndex', '-1');
+ });
+ });
});
diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsx
index c9f1086666..3a356ecdfd 100644
--- a/packages/core/src/Selector/Selector.tsx
+++ b/packages/core/src/Selector/Selector.tsx
@@ -4,7 +4,7 @@
/**
* @file Selector.tsx
- * @input Uses React, StyleX, usePopover, Icon
+ * @input Uses React, StyleX, usePopover, useTooltip, Icon
* @output Exports Selector component
* @position Core implementation; consumed by index.ts
*
@@ -27,6 +27,7 @@ import React, {
} from 'react';
import * as stylex from '@stylexjs/stylex';
import {usePopover} from '../Popover/usePopover';
+import {useTooltip} from '../Tooltip';
import {Icon, renderIconSlot, type IconType} from '../Icon';
import type {IconName} from '../Icon';
import {
@@ -369,6 +370,28 @@ interface SelectorPropsBase<
*/
isDisabled?: boolean;
+ /**
+ * Explains why the selector is disabled. When set together with
+ * `isDisabled`, the selector shows a tooltip with this text on hover and
+ * keyboard focus, and the trigger stays focusable (via `aria-disabled`)
+ * so the reason is discoverable by keyboard and assistive technology.
+ * Activation stays blocked.
+ *
+ * Use this instead of wrapping a disabled selector in `Tooltip` — disabled
+ * controls don't emit the pointer events an external tooltip needs.
+ *
+ * @example
+ * ```
+ * <Selector
+ * label="Owner"
+ * options={owners}
+ * isDisabled
+ * disabledMessage="You need the Editor role to change this"
+ * />
+ * ```
+ */
+ disabledMessage?: string;
+
/**
* The options to display in the selector.
* Can be strings, objects, dividers, or sections.
@@ -525,6 +548,7 @@ export function Selector<T extends SelectorOptionType>(
isOptional = false,
isRequired = false,
isDisabled = false,
+ disabledMessage,
options,
value,
onChange,
@@ -567,11 +591,26 @@ export function Selector<T extends SelectorOptionType>(
const [optimisticValue, setOptimisticValue] = useOptimistic(normalizedValue);
const isBusy = isLoading || optimisticValue !== normalizedValue;
+ // Disabled-reason tooltip. Disabled controls swallow pointer events, so the
+ // tooltip listeners attach to the trigger container (which already exists)
+ // and the trigger button stays perceivable via aria-disabled instead of the
+ // disabled attribute. Activation is blocked by the isDisabled guards in
+ // useCombobox (onTriggerClick / onKeyDown).
+ const showsDisabledMessage = isDisabled && !!disabledMessage;
+ const disabledMessageTooltip = useTooltip({
+ placement: 'above',
+ // The container div is not naturally focusable; focusin bubbles up from
+ // the trigger button, so always attach focus listeners.
+ focusTrigger: 'always',
+ isEnabled: showsDisabledMessage,
+ });
+
// Build aria-describedby
const ariaDescribedBy =
[
description ? descriptionId : null,
status?.message ? statusMessageId : null,
+ showsDisabledMessage ? disabledMessageTooltip.describedBy : null,
]
.filter(Boolean)
.join(' ') || undefined;
@@ -902,6 +941,10 @@ export function Selector<T extends SelectorOptionType>(
<div
ref={el => {
popover.triggerRef(el);
+ // Anchor + hover/focus listeners for the disabled-message tooltip.
+ // Handlers are gated internally by isEnabled, and anchor names
+ // compose, so attaching unconditionally is safe.
+ disabledMessageTooltip.ref(el);
}}
onClick={onTriggerClick}
data-testid={testId}
@@ -943,9 +986,13 @@ export function Selector<T extends SelectorOptionType>(
aria-required={isRequired ? 'true' : undefined}
aria-invalid={status?.type === 'error' ? 'true' : undefined}
aria-busy={isBusy || undefined}
- disabled={isDisabled}
+ // With a disabledMessage the trigger keeps focusability via
+ // aria-disabled so the reason is focus-discoverable; activation is
+ // still blocked by the isDisabled guards in useCombobox.
+ disabled={isDisabled && !showsDisabledMessage}
+ aria-disabled={showsDisabledMessage ? 'true' : undefined}
onKeyDown={onKeyDown}
- tabIndex={isDisabled ? -1 : 0}
+ tabIndex={isDisabled && !showsDisabledMessage ? -1 : 0}
{...stylex.props(styles.trigger)}>
<span {...stylex.props(styles.triggerLabel)}>
{selectedItem?.label ?? placeholder}
@@ -1012,6 +1059,9 @@ export function Selector<T extends SelectorOptionType>(
style: popoverOffsetStyle,
},
)}
+
+ {showsDisabledMessage &&
+ disabledMessageTooltip.renderTooltip(disabledMessage)}
</Field>
);
}