Winter snow for the whole page. Snowflakes fall over the content and move away from the pointer, snow piles up along the bottom of the screen, and cards you mark get snow on top and/or icicles below. The pointer shovels the snow away, and a click breaks an icicle. Typing snow, santa or snowman is a surprise, if you turn that on.
Unlike SnowBackground, which is a quiet background for a front page, this reacts to the pointer and covers the whole page.
Install
Requires @intility/bifrost-experimental, installed and set up. Then import the component and the refs you need/want:
import Snowfall, {
icicleRef,
seasonalDecorationRef,
snowCapRef,
} from "@intility/bifrost-experimental/Snowfall";Usage
Render one Snowfall anywhere in the page, for example next to the router. It draws into two fixed canvases in the body, so where it sits in your component tree doesn't matter, and it doesn't block clicks.
const App = () => (
<>
<Snowfall />
<Router />
</>
);Don't render more than one. The snow on cards is shared by all of them.
Turning it on and off
Snowfall has no switch, no stored setting and no idea of the season. You decide when it snows with enabled, or by not rendering it. A switch that remembers the choice, and the dates it snows on, are yours to add:
const [snow, setSnow] = useState(() => localStorage.getItem("snow") !== "off");
const inSeason = new Date().getMonth() >= 10; // November and December
<Snowfall enabled={snow && inSeason} />;When enabled is false, nothing is rendered and the cards lose their snow and icicles. Turning it back on starts with a clean slate.
Snow and icicles on cards
Put a ref from the package on an element to decorate it. They are callback refs, so they work on any element and on components that pass ref on to their root element, like Section and Box.
| Ref | What the element gets |
|---|---|
snowCapRef | Snow that piles up on top of the element, and that snowmen can stand on |
icicleRef | Icicles that grow slowly under the element |
seasonalDecorationRef(i) | Snow, icicles, or both, picked by the number i so neighboring cards differ |
<Section ref={snowCapRef}>...</Section>
<Section ref={icicleRef}>...</Section>
<Section ref={seasonalDecorationRef(0)}>...</Section>seasonalDecorationRef cycles through snow, icicles, and both. Give each card in a row its own number, such as the index in a list. The same number always gives the same decoration, so it is safe to call while rendering.
To get both snow and icicles on one element, use seasonalDecorationRef(2).
The decorations are drawn on a canvas inside the element, so they scroll with it and keep up even when the page is busy. Good to know:
- The canvas sticks out 14px at the sides, 28px below and 56px above the element. A parent with
overflow: hiddenoroverflow: clipcuts the decorations off. - At the sides the canvas never reaches past the edge of the screen, so a card that fills the page width doesn't cause a horizontal scrollbar.
- An element that has
position: staticis set toposition: relativewhile it is decorated, and set back afterwards. - Elements that are off screen are not redrawn.
Interaction
- Shovel. Moving the pointer over snow on the ground or on a card clears it.
- Icicles. Clicking within 40px of an icicle cracks it, and it falls and grows back.
- Snowflakes. They are pushed away from the pointer.
Easter eggs
Turn them on with easterEggs. Then anyone who types one of these words on the page gets a surprise:
| Type | What happens |
|---|---|
snow | A burst of extra snowflakes that fall once, followed by more snow piling up |
santa | Santa flies across the screen with his sleigh |
snowman | Three snowballs roll in and stack up as a snowman on a card with snow on top, or on the snow on the ground if no card is in view. Click to crumble it |
<Snowfall easterEggs />They are off by default, because they listen to the keys typed anywhere on the page. Keys typed in input fields, textareas, selects and editable content are ignored, so passwords and search text are never looked at. Only the last 7 typed characters are kept in memory, to compare with the words above. Nothing is stored or sent.
Options
Change how much snow there is with options. Leave a value out to keep its default.
<Snowfall
options={{ snowflakeCount: 120, maxSnowHeight: 80, maxSnowmen: 12 }}
/>| Option | Default | What it changes |
|---|---|---|
snowflakeCount | 50 | Snowflakes that are always falling |
burstCount | 150 | Snowflakes in the burst when snow is typed |
maxSnowHeight | 40 | How high the snow on the ground can pile up, in px |
snowCapMaxHeight | 12 | How high the snow on top of a card can pile up, in px |
icicleMaxLength | 14 | The longest an icicle can grow, in px |
maxSnowmen | 6 | Snowmen that can stand at once. The oldest one goes first |
wipeRadius | 20 | How far the pointer reaches when it shovels, in px |
accumulationSpeed | 1 | How fast snow piles up and icicles grow. 2 is twice as fast |
flakeDriftSpeed | 20 | How fast snowflakes sway sideways, in px per second. 0 is straight down |
pointerRadius | 120 | How far from the pointer snowflakes are pushed away, in px. 0 turns it off |
icicleBreakRadius | 40 | How close a click has to be to break an icicle, in px |
santaFlightDuration | 10000 | How long Santa takes to cross the screen, in ms |
Set the options once. If you change them while the snow is on, some values, like snowflakeCount and maxSnowHeight, only apply after the snow has been turned off and on again. Many snowflakes cost frame rate on slow devices.
Good to know
- The canvases are fixed to the viewport at
z-index9998 and 9999, so the snow falls in front of the page. The ground snow is behind the flakes. A modaldialogopened withshowModal()is in the top layer, and covers the snow. - In dark mode the snow is white. In light mode the snowflakes get festive colors, because white would vanish on a light page. The theme is read from the page, so a switch between light and dark mode applies without restarting.
- Decorated elements and the snow on the ground are reset when
Snowfallis turned off or unmounted.
Accessibility
- Nothing is drawn with reduced motion turned on or in forced colors mode, such as Windows high contrast. This applies at once, without a reload.
- The canvases are hidden from screen readers, and never take focus or clicks.
- There is no pause control. WCAG 2.2.2 (Pause, Stop, Hide) asks for one for animation that runs longer than 5 seconds, so offer a switch that sets
enabled, like the one in the demo, wherever the snow is on by default.