use-tilt
Produces roll/tilt values from pointer and device orientation
Source
Package
Usage
use-tilt returns roll and tilt values based on the pointer position over the target
element or, on mobile devices, the device orientation. Map them to CSS transforms to create
a tilt effect. Assign ref to a static wrapper and transform its child: an element that
rotates itself flickers near its edges.
If you need a ready-to-use component with glare and layers, use Tilt:
Move your cursor
The edge under the pointer moves away from you
source: mouse · roll: 0.00 · tilt: 0.00
Definition
roll and tilt values
roll is the vertical tilt and tilt is the horizontal tilt. Both are 0 at the center of
the element and reach -0.5 and 0.5 at its edges. roll is positive above the center,
tilt is positive to the right of it.
Multiply the values by the number of degrees you need. With rotateX(roll) and rotateY(tilt),
the edge under the pointer moves away from the viewer:
Example: 3D card
Nested layers with different multipliers create depth:
Reset on leave
By default, roll and tilt reset to 0 when the pointer leaves the target element.
Set resetOnLeave: false to keep the last values:
React to the whole page
By default, the values change only while the target element is hovered. Set trackWindow: true
to react to the pointer anywhere on the page. The values are then not clamped: they grow the
further the cursor is from the element.
Move your cursor anywhere on the page
Adjusting values
Use the adjust options to amplify, dampen, clamp or invert the values of each input source. In this example, the mouse values are inverted and amplified, so the edge under the cursor moves towards the viewer:
The edge under the cursor lifts towards you
roll: 0.00 · tilt: 0.00
Source selection
source is the active input, 'mouse' or 'deviceOrientation'.
Once the device reports orientation data, the hook switches to it and stays there.
Use source to show a hint:
Device orientation permission
On iOS, device orientation requires a permission. use-tilt does not request it: call
DeviceOrientationEvent.requestPermission() from a user event, for example a button click.
Until the permission is granted, the hook uses the pointer position:
Exported types
UseTiltOptions, UseTiltReturnValue and UseTiltSource types are exported from
the @mantine/hooks package: