Skip to content

Commit

Permalink
fix(collapsible): close animation on default open (#483)
Browse files Browse the repository at this point in the history
* fix(accordion): don't steal focus on content focus
  • Loading branch information
jer3m01 authored Sep 2, 2024
1 parent 7dd2d73 commit 8247c0b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"@kobalte/utils": "^0.9.1",
"@solid-primitives/props": "^3.1.8",
"@solid-primitives/resize-observer": "^2.0.26",
"solid-presence": "^0.1.6",
"solid-presence": "^0.1.8",
"solid-prevent-scroll": "^0.1.4"
},
"devDependencies": {
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/accordion/accordion-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface AccordionRootCommonProps<T extends HTMLElement = HTMLElement> {
ref: T | ((el: T) => void);
onKeyDown: JSX.EventHandlerUnion<T, KeyboardEvent>;
onMouseDown: JSX.EventHandlerUnion<T, MouseEvent>;
onFocusIn: JSX.EventHandlerUnion<T, FocusEvent>;
onFocusIn: JSX.EventHandlerUnion<T, FocusEvent>; // TODO: remove next breaking
onFocusOut: JSX.EventHandlerUnion<T, FocusEvent>;
}

Expand Down Expand Up @@ -102,7 +102,7 @@ export function AccordionRoot<T extends ValidComponent = "div">(
"shouldFocusWrap",
"onKeyDown",
"onMouseDown",
"onFocusIn",
"onFocusIn", // TODO: remove next breaking
"onFocusOut",
]);

Expand All @@ -122,6 +122,8 @@ export function AccordionRoot<T extends ValidComponent = "div">(
dataSource: items,
});

listState.selectionManager().setFocusedKey("item-1");

const selectableList = createSelectableList(
{
selectionManager: () => listState.selectionManager(),
Expand Down Expand Up @@ -156,8 +158,7 @@ export function AccordionRoot<T extends ValidComponent = "div">(
selectableList.onMouseDown,
])}
onFocusIn={composeEventHandlers([
local.onFocusIn,
selectableList.onFocusIn,
local.onFocusIn, // TODO: remove next breaking
])}
onFocusOut={composeEventHandlers([
local.onFocusOut,
Expand Down
24 changes: 15 additions & 9 deletions packages/core/src/collapsible/collapsible-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ export function CollapsibleContent<T extends ValidComponent = "div">(
// When opening we want it to immediately open to retrieve dimensions.
// When closing we delay `present` to retrieve dimensions before closing.
const isOpen = () => context.isOpen() || present();

let isMountAnimationPrevented = isOpen();
let originalStyles: Record<string, string> | undefined;

onMount(() => {
const raf = requestAnimationFrame(() => {
Expand All @@ -105,11 +103,6 @@ export function CollapsibleContent<T extends ValidComponent = "div">(
return;
}

originalStyles = originalStyles || {
transitionDuration: ref()!.style.transitionDuration,
animationName: ref()!.style.animationName,
};

// block any animations/transitions so the element renders at its full dimensions
ref()!.style.transitionDuration = "0s";
ref()!.style.animationName = "none";
Expand All @@ -121,10 +114,23 @@ export function CollapsibleContent<T extends ValidComponent = "div">(

// kick off any animations/transitions that were originally set up if it isn't the initial mount
if (!isMountAnimationPrevented) {
ref()!.style.transitionDuration = originalStyles.transitionDuration;
ref()!.style.animationName = originalStyles.animationName;
ref()!.style.transitionDuration = "";
ref()!.style.animationName = "";
}
},
),
);

createEffect(
on(
context.isOpen,
(open) => {
if (!open && ref()) {
ref()!.style.transitionDuration = "";
ref()!.style.animationName = "";
}
},
{ defer: true },
),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/combobox/combobox-base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ export function ComboboxBase<

const { present: contentPresent } = createPresence({
show: () => local.forceMount || disclosureState.isOpen(),
element: contentRef,
element: () => contentRef() ?? null,
});

const open = (
Expand Down
30 changes: 25 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8247c0b

Please sign in to comment.