Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ Versioning follows the policy in [CONTRIBUTING.md](CONTRIBUTING.md#versioning).
sections) can read apart in the tooltip as they do on the trigger.
`content` is never called while nothing is selected or the trigger is
disabled.
- **`Selector`: `showSelectedOptionTooltip` reads a truncated value on the
trigger (#174).** The single-select trigger cuts a long label off with an
ellipsis, with no way to read it whole. With `showSelectedOptionTooltip`,
hovering or focusing the trigger shows the selected option's whole label in
a `Tooltip`, from `options` (sections included), and only while the trigger
actually clips it: a label that fits adds no tooltip. There is none while
nothing is selected, while the control is disabled (its `disabledMessage`
keeps the trigger), or while the listbox is open, and `onOpenChange` is
still called. The tooltip's id joins the trigger's `aria-describedby` beside
the caller's. Off by default, so existing call sites render as before. The
tooltip follows the trigger's own disabled and open state exactly as
`MultiSelector`'s `showSelectedOptionsTooltip` does, through one shared
implementation.

### Fixed

Expand Down
62 changes: 3 additions & 59 deletions src/formControls/MultiSelector/MultiSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* `showSelectedOptionsTooltip` lists the selected labels in a tooltip on the trigger, or
* renders the caller's `content` for the selection.
*/
import { useLayoutEffect, useState, type ReactNode } from "react";
import { useState, type ReactNode } from "react";
import {
MultiSelector as AstryxMultiSelector,
type MultiSelectorDivider,
Expand All @@ -22,8 +22,8 @@ import {
import { useMergedRefs } from "@astryxdesign/core/hooks";
import type { SelectorOptionType } from "@astryxdesign/core/Selector";
import { getSelectableOptions } from "@astryxdesign/core/Selector/utils";
import { useTooltip } from "@astryxdesign/core/Tooltip";

import { TriggerTooltip } from "../triggerTooltip";
import {
EMPTY_ARRAY,
FormControlScope,
Expand Down Expand Up @@ -171,7 +171,7 @@ export function MultiSelector<
<FormControlScope required={required}>
<AstryxMultiSelector {...props} />
{hasTooltip && showSelectedOptionsTooltip && trigger ? (
<SelectionTooltip
<TriggerTooltip
trigger={trigger}
renderContent={() =>
tooltipContent(
Expand All @@ -194,59 +194,3 @@ function tooltipContent(
return items.map((item) => item.label).join(", ");
return showSelectedOptionsTooltip.content(items);
}

/** What the tooltip needs from the trigger: disabled, and the listbox open. */
const readTrigger = (trigger: HTMLElement) => ({
// A disabled InputGroup disables the trigger through context Astryx does not
// export; the trigger's aria-disabled is where it shows.
isDisabled: trigger.getAttribute("aria-disabled") === "true",
isExpanded: trigger.getAttribute("aria-expanded") === "true",
});

/**
* The selection tooltip, mounted only with `showSelectedOptionsTooltip`. It follows the
* trigger's own attributes, so it is held shut while the listbox is open and
* absent while the trigger is disabled, from the first commit on.
*/
function SelectionTooltip({
trigger,
renderContent,
onDescribedBy,
}: {
trigger: HTMLElement;
/** Called only while the tooltip is enabled, so a disabled trigger never runs it. */
renderContent: () => ReactNode;
onDescribedBy: (id: string | undefined) => void;
}) {
const [state, setState] = useState(() => readTrigger(trigger));
useLayoutEffect(() => {
const sync = () =>
setState((prev) => {
const next = readTrigger(trigger);
return prev.isDisabled === next.isDisabled &&
prev.isExpanded === next.isExpanded
? prev
: next;
});
sync();
const observer = new MutationObserver(sync);
observer.observe(trigger, { attributeFilter: ["aria-disabled", "aria-expanded"] });
return () => observer.disconnect();
}, [trigger]);
const isEnabled = !state.isDisabled;
const tooltip = useTooltip({
isEnabled,
isOpen: state.isExpanded ? false : undefined,
});
const attach = tooltip.ref;
useLayoutEffect(
() => (isEnabled ? attach(trigger) : undefined),
[attach, trigger, isEnabled],
);
const { describedBy } = tooltip;
useLayoutEffect(() => {
onDescribedBy(isEnabled ? describedBy : undefined);
return () => onDescribedBy(undefined);
}, [onDescribedBy, describedBy, isEnabled]);
return isEnabled ? tooltip.renderTooltip(renderContent()) : null;
}
21 changes: 21 additions & 0 deletions src/formControls/Selector/Selector.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,27 @@ export const DisabledOptionReason: Story = {
},
};

/**
* `showSelectedOptionTooltip`: hover or focus the trigger to read the selected
* option's whole label when the trigger cuts it short with an ellipsis. A label
* that fits shows no tooltip, and neither does an empty or disabled control.
*/
export const ShowTruncatedValueTooltip: Story = {
decorators: [withMaxWidth(200)],
args: {
showSelectedOptionTooltip: true,
options: [
{ value: "north", label: "North" },
{ value: "south", label: "South" },
{
value: "ap-northeast-2",
label: "ap-northeast-2 (Seoul, South Korea)",
},
],
defaultValue: "ap-northeast-2",
},
};

/** On its own, disabled. */
export const Disabled: Story = {
args: { disabled: true },
Expand Down
Loading
Loading