-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(alert): add expand and collapse behaviour to inline alert (#114)
* refactor(expandable): extract transition animation from Expandable Create AnimatedExpansion component that handles expand-collapse animation in Expandable, making it possible to easily add this behaviour to other components. * feat(alert): add expansion and collapsion behaviour to inline alert Wrap Alert in AnimatedExpansion component to make it appear and disappear smoothly * feat(alert): remove neutral variant of inline alert
- Loading branch information
Showing
7 changed files
with
150 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { PropsWithChildren, useEffect, useRef, useState } from 'react'; | ||
import { collapse, expand } from 'element-collapse'; | ||
import { classNames } from '@chbphone55/classnames'; | ||
|
||
export function AnimatedExpansion({ | ||
show, | ||
children, | ||
}: PropsWithChildren<{ show?: Boolean }>) { | ||
const [isExpanded, setIsExpanded] = useState(show); | ||
const expandableRef = useRef(null); | ||
const isMounted = useRef(false); | ||
|
||
async function collapseElement() { | ||
await new Promise((resolve) => { | ||
collapse(expandableRef.current, resolve); | ||
}); | ||
setIsExpanded(false); | ||
} | ||
|
||
function expandElement() { | ||
expand(expandableRef.current); | ||
setIsExpanded(true); | ||
} | ||
|
||
useEffect(() => { | ||
// Don't do anything at first render | ||
if (!isMounted.current) { | ||
isMounted.current = true; | ||
return; | ||
} | ||
|
||
if (show) { | ||
expandElement(); | ||
} else { | ||
collapseElement(); | ||
} | ||
}, [show]); | ||
|
||
return ( | ||
<div | ||
ref={expandableRef} | ||
className={classNames({ | ||
'overflow-hidden': true, | ||
'h-0 invisible': !isExpanded, | ||
})} | ||
aria-hidden={!isExpanded} | ||
> | ||
{children} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { Clickable } from './clickable'; | ||
export { DeadToggle } from './dead-toggle'; | ||
export { Affix } from './affix'; | ||
export { AnimatedExpansion } from './animated-expansion'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.