use-tilt
Produces roll/tilt values from pointer and device orientation
Source
Docs
Package
Usage
use-tilt returns normalized roll and tilt values based on the pointer position over
the target element or, on supported mobile devices, the device orientation.
Map these values to CSS transforms to create a tilt effect:
Move your cursor
The edge under the pointer moves away from you
source: mouse · roll: 0.00 · tilt: 0.00
Assign ref to a static wrapper and apply the transform to its child. If the element
with ref rotates itself, it flickers near its edges.
If you need a ready-to-use component with glare and layers, use the Tilt component, which is based on this hook.
Definition
roll and tilt values
roll is the vertical tilt and tilt is the horizontal tilt:
- Mouse: both values are
0at the center of the element and approach-0.5/0.5at the edges.rollis positive above the center,tiltis positive to the right of the center. - Device orientation:
rollis-beta / 90andtiltisgamma / 90.
Multiply the values by the number of degrees or pixels 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 a sense of 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 hook updates the values only while the target element is hovered.
Set trackWindow: true to react to the pointer position anywhere on the page:
Move your cursor anywhere on the page
With trackWindow, the values are not clamped to [-0.5, 0.5] – they grow
the further the cursor is from the element. Use the adjust options to limit the effect.
Adjusting values
Use the *Adjust options to amplify, dampen, clamp or invert the values of each input
source. In the example below, 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 deviceOrientation and keeps using it.
Use source to display input-specific hints, for example "move the cursor" or "tilt the device".
Device orientation permission
On iOS 13+, access to device orientation requires a permission. use-tilt does not request
it – call DeviceOrientationEvent.requestPermission() from an event handler, 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, you can import them in your application: