# Scale Feedback
URL: /react/components/concepts/scale-feedback
Source: https://github.com/daangn/seed-design/blob/dev/docs/content/react/components/concepts/scale-feedback.mdx
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`.
```tsx title="MyButton.tsx"
import { ScaleFeedback } from "@seed-design/react";
function MyButton({ className, ...props }) {
return (
{/* [!code highlight] */}
);
}
```
`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](/react/components/concepts/composition) documentation.
#### Using without `ScaleFeedback`
If you can't use `ScaleFeedback`, use `useScaleFeedback` instead. Pass `scaleFeedbackRef` and `scaleFeedbackClassName` to the same element.
```tsx title="MyButton.tsx"
import { useScaleFeedback } from "@seed-design/react";
import clsx from "clsx";
function MyButton({ className, ...props }) {
const { scaleFeedbackRef, scaleFeedbackClassName } = useScaleFeedback();
return (
);
}
```
### 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`.
```tsx title="MyButton.tsx"
```
#### 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)`.
```css title="style.css"
@import "tailwindcss";
@utility feedback-scale {
scale: var(--seed-feedback-scale);
}
```
```tsx title="MyButton.tsx"
```
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.
```tsx title="MyButton.tsx"
```
#### 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:var(--seed-feedback-scale)]`.
```js title="tailwind.config.js"
import plugin from "tailwindcss/plugin";
export default {
plugins: [
plugin(({ addUtilities }) => {
addUtilities({
".feedback-scale": { scale: "var(--seed-feedback-scale)" },
});
}),
],
};
```
```tsx title="MyButton.tsx"
```
Register with `addUtilities`, not `theme.extend.scale`. Tailwind CSS 3's `scale-*` utility applies `scaleX()` and `scaleY()` to the `transform` property (not the [individual `scale` property](#individual-scale-property)), so it conflicts with other transforms on the same element.
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.
```ts title="myButton.css.ts"
import { style } from "@vanilla-extract/css";
import {
feedbackScale,
feedbackScaleTransition,
} from "@seed-design/css/scale-feedback";
import { vars } from "@seed-design/css/vars";
export const myButton = style({
backgroundColor: vars.$color.bg.brandSolid,
transition: `background-color 0.2s, ${feedbackScaleTransition}`, // [!code highlight]
selectors: {
"&:active": {
scale: feedbackScale, // [!code highlight]
},
},
});
```
```tsx title="MyButton.tsx"
```
```css title="MyButton.css"
.my-button {
transition:
background-color 0.2s,
var(--seed-feedback-scale-transition);
}
.my-button:active {
scale: var(--seed-feedback-scale);
}
```
#### 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](/react/components/concepts/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`](https://developer.mozilla.org/en-US/docs/Web/CSS/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.
```tsx
import type { ComponentPropsWithoutRef } from "react";
import {
IconBookmarkLine,
IconChevronRightLine,
IconClockLine,
IconDot3HorizontalLine,
} from "@karrotmarket/react-monochrome-icon";
import { HStack, Icon, ScaleFeedback, VStack } from "@seed-design/react";
import clsx from "clsx";
function PressableCard({ className, ...props }: ComponentPropsWithoutRef<"button">) {
return (
);
}
export default function ScaleFeedbackCustomComponent() {
return (
Ad anim deserunt
Consequat ea commodo nisi eiusmod ex et est.
} size="x4" color="fg.neutralMuted" />
} size="x5" color="fg.neutral" />
} size="x5" color="fg.neutral" />
} size="x5" color="fg.neutral" />
);
}
```
## Automatically handled
You don't need to handle the following two cases separately:
- **Reduced motion setting**: In `prefers-reduced-motion: reduce` environments, `--seed-feedback-scale` is always `1`.
- **Unknown size**: If JavaScript hasn't run or before the first measurement, `--seed-feedback-scale` is `1`. 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.
- [Checkbox](/react/components/checkbox#scale-feedback)
- [RadioGroup](/react/components/radio-group#scale-feedback)
- [Switch](/react/components/switch#scale-feedback)
### `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`](/react/getting-started/styling/cascade-layers) 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.
```css title="scale-feedback-off.css"
.seed-scale-feedback {
--seed-feedback-scale: 1;
}
```
The default `scale: 1` remains, so the positioning reference for [`position: fixed` descendants](#position-fixed-descendants) doesn't change.