Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug in AnimatedExpansion #119

Merged
merged 1 commit into from
Jun 8, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import React, { PropsWithChildren, useEffect, useRef, useState } from 'react';
import { collapse, expand } from 'element-collapse';
import { classNames } from '@chbphone55/classnames';

export function AnimatedExpansion({
export function ExpandTransition({
show,
children,
}: PropsWithChildren<{ show?: Boolean }>) {
const [isExpanded, setIsExpanded] = useState(show);
const expandableRef = useRef(null);
const expandableRef = useRef<HTMLDivElement>(null);
const isMounted = useRef(false);

async function collapseElement() {
async function collapseElement(el: HTMLElement) {
await new Promise((resolve) => {
collapse(expandableRef.current, resolve);
collapse(el, resolve);
});
setIsExpanded(false);
}

function expandElement() {
expand(expandableRef.current);
function expandElement(el: HTMLElement) {
expand(el);
setIsExpanded(true);
}

Expand All @@ -29,10 +29,12 @@ export function AnimatedExpansion({
return;
}

if (!expandableRef.current) return;

if (show) {
expandElement();
expandElement(expandableRef.current);
} else {
collapseElement();
collapseElement(expandableRef.current);
}
}, [show]);

Expand Down
2 changes: 1 addition & 1 deletion packages/_helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { Clickable } from './clickable';
export { DeadToggle } from './dead-toggle';
export { Affix } from './affix';
export { AnimatedExpansion } from './animated-expansion';
export { ExpandTransition } from './expand-transition';
6 changes: 3 additions & 3 deletions packages/alert/src/component.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { classNames } from '@chbphone55/classnames';
import React, { PropsWithChildren, ReactElement } from 'react';
import { AlertProps } from '.';
import { AnimatedExpansion } from '../../_helpers';
import { ExpandTransition } from '../../_helpers';

export function Alert({
show,
Expand All @@ -12,7 +12,7 @@ export function Alert({
const { color, icon } = styleMap[type];

return (
<AnimatedExpansion show={show}>
<ExpandTransition show={show}>
<div
className={classNames(
props.className,
Expand All @@ -23,7 +23,7 @@ export function Alert({
<div className={`mr-8 text-${color}-600`}>{icon}</div>
<div className="last-child:mb-0 text-14">{children}</div>
</div>
</AnimatedExpansion>
</ExpandTransition>
);
}

Expand Down
9 changes: 6 additions & 3 deletions packages/expandable/src/component.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { classNames } from '@chbphone55/classnames';
import { box as boxClasses, buttonReset } from '@fabric-ds/css/component-classes';
import {
box as boxClasses,
buttonReset,
} from '@fabric-ds/css/component-classes';
import React from 'react';
import { AnimatedExpansion } from '../../_helpers';
import { ExpandTransition } from '../../_helpers';
import type { ExpandableProps } from './props';

export function Expandable(props: ExpandableProps) {
Expand Down Expand Up @@ -92,7 +95,7 @@ export function Expandable(props: ExpandableProps) {

function ExpansionBehaviour({ animated, stateExpanded, children }) {
return animated ? (
<AnimatedExpansion show={stateExpanded}>{children}</AnimatedExpansion>
<ExpandTransition show={stateExpanded}>{children}</ExpandTransition>
) : (
<div
className={classNames({
Expand Down