Skip to content
Draft
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
1 change: 1 addition & 0 deletions .stylelintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
],
"plugins": ["stylelint-selector-bem-pattern"],
"rules": {
"at-rule-no-unknown": [true, { "ignoreAtRules": ["starting-style"] }],
"no-descending-specificity": null,
"selector-class-pattern": null,
"plugin/selector-bem-pattern": {
Expand Down
37 changes: 22 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,19 @@ function MyModal() {

Custom transition, focus management and hash-based state management.

Use your favorite animation library, [@wethegit/react-hooks](https://wethegit.github.io/react-hooks/use-animate-presence) provides a simple one for these cases.
The [`@starting-style`](https://developer.mozilla.org/en-US/docs/Web/CSS/@starting-style) CSS at-rule lets you define entry animations with pure CSS — no extra JavaScript or animation libraries needed.

```jsx
import { useRef } from 'react'
import { useAnimatePresence } from '@wethegit/react-hooks'
import {
Modal,
ModalContent,
ModalBackdrop,
useModal
} from "@wethegit/react-modal"

import styles from "./my-styles.module.css"

function MyModal() {
const triggerButton = useRef(null)
const modalRootRef = useRef(null)
Expand All @@ -124,27 +125,17 @@ function MyModal() {
hash: "modal-with-hash",
})

const { render, animate } = useAnimatePresence({
isVisible: isOpen,
duration: 800
})

return (
<>
<button ref={triggerButton} onClick={toggle}>
Open the modal window!
</button>
<div ref={modalRootRef}></div>

{render && modalRootRef.current && (
<Modal
renderTo={modalRootRef.current}
style={{
transition: `opacity 800ms ease-in-out`,
opacity: animate ? 1 : 0
}}>
{isOpen && modalRootRef.current && (
<Modal renderTo={modalRootRef.current}>
<ModalBackdrop onClick={toggle} />
<ModalContent>
<ModalContent className={styles.modal}>
<button onClick={toggle}>
Close
</button>
Expand All @@ -157,6 +148,22 @@ function MyModal() {
}
```

```css
/* my-styles.module.css */
.modal {
transition: opacity 0.5s ease, transform 0.5s ease;
opacity: 1;
transform: scale(1);
}

@starting-style {
.modal {
opacity: 0;
transform: scale(0.9);
}
}
```

## Props

### `<Modal>`
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

12 changes: 6 additions & 6 deletions src/main.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
}

.CustomModalTransition {
transition: transform var(--transition-duration)
var(--ease, cubic-bezier(0.77, 0, 0.1, 1.39));
transform: scale(var(--scale, 0));
transition: transform 0.5s cubic-bezier(0.77, 0, 0.1, 1.39);
transform: scale(1);
}

.CustomModalTransitionOpen {
--ease: cubic-bezier(0.77, 0, 0.1, 1.39);
--scale: 1;
@starting-style {
.CustomModalTransition {
transform: scale(0);
}
}

.CustomModalAbsolute {
Expand Down
7 changes: 6 additions & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ function CustomModal() {
{isOpen && modalRootRef.current && (
<Modal renderTo={modalRootRef.current}>
<ModalBackdrop onClick={toggle} className={styles.CustomModalOverlay} />
<ModalContent className={classnames([styles.CustomModalContent])}>
<ModalContent
className={classnames([
styles.CustomModalContent,
styles.CustomModalTransition,
])}
>
<button onClick={toggle} className={styles.CustomModalClose}>
Close
</button>
Expand Down
Loading