仕組み
Just before React applies a new viewKey, the component takes a still copy of the old view (a DOM clone with ids removed and form values, canvases and media frozen) and measures it once. After the change, the copy is laid over the new view with the nearest background colour behind it, and clipped with clip-path: path() made of one square per cell. The keyframes are computed once and played with the Web Animations API, which interpolates them because every frame has the same commands. A cell's start is its grid distance from the origin (|dx| + |dy|), giving a diagonal front from a corner and a diamond from a point, plus some randomness (jitter); once started it shrinks around its centre and drops by a third of a cell, slowly at first and then fast, so the cells at the front are still large. The copy never takes the pointer, so the new view can be used at once, and it is removed when the wave has passed. A new viewKey during a wave ends it at once and starts the next from what is in place.
向いている場面
- A light/dark switch that should feel like a moment, on a landing page, a portfolio or the settings of a consumer app.
- Changing between a few views of one card or panel: tabs, steps of a flow, before and after.
- A page change in a small site where the old page should visibly give way to the new one.
向かない場面
- Frequent, routine changes (paging a table, filtering a list): a 0.7s effect each time gets in the way.
- Views with live video, iframes or WebGL canvases, which go blank in the still copy.
- When data-theme is set on <html> before the change is rendered: the copy would already have the new colours. Put the attribute inside the wrapper.
使い方
import { PixelDissolve } from './PixelDissolve.jsx'
export function ThemedApp({ theme, children }) {
// Key the wrapper on the theme and keep data-theme inside it, so the old look is what dissolves
return (
<PixelDissolve viewKey={theme} origin="top-right">
<div data-theme={theme}>{children}</div>
</PixelDissolve>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
viewKey | string | number | — | Identifies what is showing. Each change starts a dissolve from the view that was showing to the children now passed. |
children | ReactNode | — | The live view. It is never remounted; for a theme switch put the data-theme attribute inside it. |
origin | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'center' | { clientX: number, clientY: number } | 'top-left' | Where the wave starts: a corner, the centre, or a point in viewport coordinates such as the centre of the button that was pressed. |
cell | number | — | Cell size in px. By default about one fourteenth of the longer side, at least 14px; it grows if the view would need more than 640 cells. |
duration | number | 700 | Length of the whole wave in ms. |
jitter | number | 0.35 | How ragged the front is, from 0 (a clean diagonal) to 1 (random). |
onDone | () => void | — | Called when a wave has passed and the copy of the old view is removed. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the root <div>. |
アクセシビリティ
- The new view is in place at once and takes focus, clicks and screen readers from the first frame; the copy of the old view is inert, aria-hidden and ignores the pointer.
- Focus stays where it was, because the live children are never remounted.
- Under reduced motion, or where clip-path: path() is not supported, the swap is instant with no copy at all.
- The wave carries no information; announce a theme or view change the same way you would without it (a pressed state, a selected tab).
モーション
700ms by default, played linearly through keyframes every 25ms. Cells start over the first 62% of the time and each takes the remaining 38% to go, shrinking on a quadratic ease-in while dropping a third of a cell.
プロジェクトに追加
Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。
PixelDissolve.jsxpixel-dissolve.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。