Skip to content

Commit

Permalink
Document how to use Dialog with a TransitionComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Apr 14, 2023
1 parent eb304f3 commit 0cf42f9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/pattern-library/components/patterns/FadeComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import classnames from 'classnames';
import { useCallback } from 'preact/hooks';

import type { TransitionComponent } from '../../../types';

const FadeComponent: TransitionComponent = ({
children,
visible,
onTransitionEnd,
}) => {
const handleTransitionEnd = useCallback(
() => onTransitionEnd?.(visible ? 'in' : 'out'),
[visible, onTransitionEnd]
);

return (
<div
className={classnames('transition-opacity duration-500', {
'opacity-100': visible,
'opacity-0': !visible,
})}
// @ts-expect-error react uses "ontransitionend" rather than "onTransitionEnd".
// eslint-disable-next-line react/no-unknown-property
ontransitionend={handleTransitionEnd}
>
{children}
</div>
);
};

export default FadeComponent;
32 changes: 32 additions & 0 deletions src/pattern-library/components/patterns/feedback/DialogPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Scroll,
} from '../../../../next';
import Library from '../../Library';
import FadeComponent from '../FadeComponent';
import { LoremIpsum, nabokovNovels } from '../samples';

const nabokovRows = nabokovNovels();
Expand Down Expand Up @@ -322,6 +323,37 @@ export default function DialogPage() {
</Dialog_>
</Library.Demo>
</Library.Example>

<Library.Example title="transitionComponent">
<p>
It allows to provide a component which supports transitions, but
keeping the internal behavior (initial focus, closing, etc)
transparent to consumers.
</p>
<p>
The only requirement is that provided component needs to conform
to the next props:
</p>
<Library.Code
content={`type TransitionComponentProps = {
visible: boolean;
onTransitionEnd?: (direction: 'in' | 'out') => void;
}`}
/>
<Library.Demo
title="Dialog with TransitionComponent example"
withSource
>
<Dialog_
title="Dialog with TransitionComponent example"
transitionComponent={FadeComponent}
>
<p>
This dialog has a fade in/out transition when opened/closed.
</p>
</Dialog_>
</Library.Demo>
</Library.Example>
</Library.Pattern>

<Library.Pattern title="Forwarded Props: Panel">
Expand Down

0 comments on commit 0cf42f9

Please sign in to comment.