Skip to content

Commit

Permalink
WIP Add keepMounted prop to Collapsible
Browse files Browse the repository at this point in the history
  • Loading branch information
mj12albert committed Nov 28, 2024
1 parent 5e827dc commit d089d08
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/src/app/experiments/collapsible.module.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.wrapper {
--width: 320px;
--duration: 300ms;
--duration: 1000ms;

font-family: system-ui, sans-serif;
line-height: 1.4;
Expand Down
18 changes: 15 additions & 3 deletions packages/react/src/collapsible/panel/CollapsiblePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ const CollapsiblePanel = React.forwardRef(function CollapsiblePanel(
props: CollapsiblePanel.Props,
forwardedRef: React.ForwardedRef<HTMLButtonElement>,
) {
const { className, hiddenUntilFound, render, ...otherProps } = props;
const { className, hiddenUntilFound, keepMounted = false, render, ...otherProps } = props;

const { animated, mounted, open, panelId, setPanelId, setMounted, setOpen, state } =
useCollapsibleRootContext();

const { getRootProps, height, width } = useCollapsiblePanel({
const { getRootProps, height, width, isOpen } = useCollapsiblePanel({
animated,
hiddenUntilFound,
id: panelId,
Expand Down Expand Up @@ -55,6 +55,10 @@ const CollapsiblePanel = React.forwardRef(function CollapsiblePanel(
customStyleHookMapping: collapsibleStyleHookMapping,
});

if (!keepMounted && !isOpen) {
return null;
}

return renderElement();
});

Expand All @@ -63,7 +67,15 @@ export { CollapsiblePanel };
namespace CollapsiblePanel {
export interface Props
extends BaseUIComponentProps<'div', CollapsibleRoot.State>,
Pick<useCollapsiblePanel.Parameters, 'hiddenUntilFound'> {}
Pick<useCollapsiblePanel.Parameters, 'hiddenUntilFound'> {
/**
* If `true`, the panel remains mounted when closed and is instead
* hidden using the `hidden` attribute
* If `false`, the panel is unmounted when closed.
* @default false
*/
keepMounted?: boolean;
}
}

CollapsiblePanel.propTypes /* remove-proptypes */ = {
Expand Down
7 changes: 6 additions & 1 deletion packages/react/src/collapsible/panel/useCollapsiblePanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,9 @@ export function useCollapsiblePanel(
getRootProps,
height,
width,
isOpen,
}),
[getRootProps, height, width],
[getRootProps, height, width, isOpen],
);
}

Expand Down Expand Up @@ -322,5 +323,9 @@ export namespace useCollapsiblePanel {
) => React.ComponentPropsWithRef<'button'>;
height: number;
width: number;
/**
* The open state of the panel, that accounts for animation/transition status.
*/
isOpen: boolean;
}
}

0 comments on commit d089d08

Please sign in to comment.