Scale Feedback
Learn how to apply Crispy's Scale Feedback to custom components.
Crispy components provide Scale Feedback by default, where elements slightly shrink when pressed. To ensure that a small icon button and a button that fills the viewport width feel the same when pressed, the scale factor is calculated differently for each element. You can apply the same effect to custom components.
How scale is determined
Instead of setting a fixed scale factor, the scale is calculated from the actual size of the element.
| Element | Width × Height | Calculated scale | Actual reduction |
|---|---|---|---|
| Icon button | 32 × 32 | 0.938 | 2px width, 2px height |
| Text button | 120 × 48 | 0.958 | 5px width, 2px height |
| Wide button | 343 × 48 | 0.977 | 8px width, 1.1px height |
| Small button | 22 × 22 | 0.917 | 1.8px width, 1.8px height |
Since the scale is calculated from the rendered size, you don't need to adjust values separately even if elements are rendered at unexpected sizes due to font scaling, screen width differences, wrapping, etc.
Applying it
ScaleFeedback handles measuring the element's size, and each use case directly defines when and which elements should apply the calculated scale factor.
Measuring element size
Wrap the element to be scaled with ScaleFeedback.
ScaleFeedback doesn't render a DOM element; it passes ref and className to the child element. Any existing ref and className on the child element are preserved.
There must be exactly one child element, and it must forward the received ref and className to its DOM element. If you're using a custom component as a child, also check the composition principles in the Composition documentation.
Using without ScaleFeedback
If you can't use ScaleFeedback, use useScaleFeedback instead. Pass scaleFeedbackRef and scaleFeedbackClassName to the same element.
Using the calculated scale
The calculated scale is exposed as the --seed-feedback-scale CSS variable, and the transition as --seed-feedback-scale-transition.
If writing styles with JS, you can import feedbackScale and feedbackScaleTransition from @seed-design/css/scale-feedback.
Using utility classes
If you apply the scale to multiple components, register a utility in your project. You can use active:feedback-scale instead of active:scale-(--seed-feedback-scale).
Register with @utility, not @theme. Items in @theme have their values fixed at :root, so the element can't reach the scale it calculated for itself. The class is generated but the scaling doesn't happen.
You can't bundle transitions the same way. The transition-* utility applies Tailwind's own duration and timing function, overwriting the value in --seed-feedback-scale-transition. Use the arbitrary property syntax as shown above.
Choosing selectors
Crispy components change color on hover for interactive elements and apply Scale Feedback while the left mouse button is pressed. For consistency, it's recommended to maintain this standard in custom components as well. See the Interaction States documentation for details.
:active is applied to disabled elements too. If your component handles disabled states, exclude them from the selector. Crispy components exclude :disabled, [disabled], and [data-disabled].
Individual scale property
Crispy components apply scaling using the individual transform property scale, not transform: scale(). Use scale in custom components as well.
transform is a shorthand and only holds one value per element. If you apply scaling here, you also have to write the element's translation and rotation to the same value, and you'll need to maintain the scaling value every time you add new transforms. The individual scale property works independently from translate and rotate, eliminating this burden. Since ScaleFeedback already uses scale, scaling stays in one property.
The individual scale property is supported in Chrome 104, Safari 14.1, and Firefox 72 and later. On earlier versions, only the scaling doesn't appear; other styles work normally.
When you apply both steps, it works as shown below. Try pressing two elements of different sizes to compare.
Automatically handled
You don't need to handle the following two cases separately:
- Reduced motion setting: In
prefers-reduced-motion: reduceenvironments,--seed-feedback-scaleis always1. - Unknown size: If JavaScript hasn't run or before the first measurement,
--seed-feedback-scaleis1. Instead of applying an incorrect scale, the effect just doesn't appear.
Things to know
Nested Crispy components
When a custom component wraps Checkmark, Radiomark, Switchmark, etc., both the outer and inner elements shrink together, causing the inner element to scale down twice. How to disable this is documented in each component's documentation.
position: fixed descendants
An element with a scale value becomes a stacking context and containing block for position: fixed descendants. If this element contains a position: fixed element, it will be positioned relative to this element, not the viewport.
An element with ScaleFeedback maintains scale: 1 even when not pressed, so descendants don't move while you're pressing. However, the positioning reference itself changes from the viewport, so if you have a position: fixed descendant below an element with scale applied, verify it appears at the intended position.
Disabling globally
Add a rule that fixes the scale to 1 without using @layer and load it after Crispy CSS. Since all places that read the scale go through this one variable, both Crispy and custom components turn off together.
The default scale: 1 remains, so the positioning reference for position: fixed descendants doesn't change.