Skip to content

Commit

Permalink
fix(rac): Register focus-visible variant in Aria components
Browse files Browse the repository at this point in the history
Change-Id: Ib3b22b1c9d5c257208e97f1310f9bd45bcaafd28
GitOrigin-RevId: 2a429b8b2082b5b8f437478d930fe0fbbe9234a7
  • Loading branch information
sarahsga authored and actions-user committed Nov 21, 2024
1 parent 7629d4a commit b2568a9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
12 changes: 9 additions & 3 deletions plasmicpkgs/react-aria/src/registerInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { pickAriaComponentVariants, WithVariants } from "./variant-utils";

const INPUT_VARIANTS = [
"focused" as const,
"focusVisible" as const,
"hovered" as const,
"disabled" as const,
];
Expand Down Expand Up @@ -84,14 +85,19 @@ export function BaseInput(props: BaseInputProps) {
hovered: isHovered,
});
}}
onFocus={() => {
plasmicUpdateVariant?.({
focused: true,
onFocus={(e) => {
setTimeout(() => {
// using settimeout to update the variant, as the input only gets the `data-focus-visible` attribute in the next tick
plasmicUpdateVariant?.({
focused: true,
focusVisible: !!e.target.getAttribute("data-focus-visible"),
});
});
}}
onBlur={() => {
plasmicUpdateVariant?.({
focused: false,
focusVisible: false,
});
}}
{...mergedProps}
Expand Down
26 changes: 24 additions & 2 deletions plasmicpkgs/react-aria/src/registerListBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@ import {
Registerable,
registerComponentHelper,
} from "./utils";
import { pickAriaComponentVariants, WithVariants } from "./variant-utils";

export interface BaseListBoxControlContextData {
itemIds: string[];
isStandalone: boolean;
}

const LISTBOX_VARIANTS = ["focused" as const, "focusVisible" as const];

const { variants } = pickAriaComponentVariants(LISTBOX_VARIANTS);

export interface BaseListBoxProps
extends Omit<
React.ComponentProps<typeof ListBox>,
"selectedKeys" | "defaultSelectedKeys"
>,
HasControlContextData<BaseListBoxControlContextData> {
HasControlContextData<BaseListBoxControlContextData>,
WithVariants<typeof LISTBOX_VARIANTS> {
children?: React.ReactNode;
selectedKeys?: string | string[] | undefined;
defaultSelectedKeys?: string | string[] | undefined;
Expand Down Expand Up @@ -55,12 +61,12 @@ export function BaseListBox(props: BaseListBoxProps) {
children,
selectedKeys,
defaultSelectedKeys,
plasmicUpdateVariant,
...rest
} = props;
const context = React.useContext(PlasmicListBoxContext);
const isStandalone = !context;
const [ids, setIds] = useState<string[]>([]);

const idManager = useMemo(
() => context?.idManager ?? new ListBoxItemIdManager(),
[]
Expand All @@ -83,6 +89,21 @@ export function BaseListBox(props: BaseListBoxProps) {
<ListBox
selectedKeys={normalizeSelectedKeys(selectedKeys)}
defaultSelectedKeys={normalizeSelectedKeys(defaultSelectedKeys)}
onFocus={(e) => {
setTimeout(() => {
// using settimeout to update the variant, as it only gets the `data-focus-visible` attribute in the next tick
plasmicUpdateVariant?.({
focused: true,
focusVisible: !!e.target.getAttribute("data-focus-visible"),
});
});
}}
onBlur={() => {
plasmicUpdateVariant?.({
focused: false,
focusVisible: false,
});
}}
{...rest}
>
{children}
Expand Down Expand Up @@ -174,6 +195,7 @@ export function registerListBox(
displayName: "Aria ListBox",
importPath: "@plasmicpkgs/react-aria/skinny/registerListBox",
importName: "BaseListBox",
variants,
defaultStyles: {
width: "250px",
borderWidth: "1px",
Expand Down
21 changes: 20 additions & 1 deletion plasmicpkgs/react-aria/src/registerSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ export interface BaseSelectControlContextData {
itemIds: string[];
}

const SELECT_VARIANTS = ["disabled" as const];
const SELECT_VARIANTS = [
"focused" as const,
"focusVisible" as const,
"disabled" as const,
];

const { variants: SELECT_VARIANTS_DATA } =
pickAriaComponentVariants(SELECT_VARIANTS);
Expand Down Expand Up @@ -139,6 +143,21 @@ export function BaseSelect(props: BaseSelectProps) {
name={name}
disabledKeys={disabledKeys}
aria-label={ariaLabel}
onFocus={(e) => {
setTimeout(() => {
// using settimeout to update the variant, as it only gets the `data-focus-visible` attribute in the next tick
plasmicUpdateVariant?.({
focused: true,
focusVisible: !!e.target.getAttribute("data-focus-visible"),
});
});
}}
onBlur={() => {
plasmicUpdateVariant?.({
focused: false,
focusVisible: false,
});
}}
{...extractPlasmicDataProps(props)}
>
<SelectAutoOpen {...props} />
Expand Down
12 changes: 9 additions & 3 deletions plasmicpkgs/react-aria/src/registerTextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { pickAriaComponentVariants, WithVariants } from "./variant-utils";

const TEXTAREA_VARIANTS = [
"focused" as const,
"focusVisible" as const,
"hovered" as const,
"disabled" as const,
];
Expand Down Expand Up @@ -59,14 +60,19 @@ export function BaseTextArea(props: BaseTextAreaProps) {

return (
<TextArea
onFocus={() => {
plasmicUpdateVariant?.({
focused: true,
onFocus={(e) => {
setTimeout(() => {
// using settimeout to update the variant, as it only gets the `data-focus-visible` attribute in the next tick
plasmicUpdateVariant?.({
focused: true,
focusVisible: !!e.target.getAttribute("data-focus-visible"),
});
});
}}
onBlur={() => {
plasmicUpdateVariant?.({
focused: false,
focusVisible: false,
});
}}
onHoverChange={(isHovered) => {
Expand Down

0 comments on commit b2568a9

Please sign in to comment.