[ @sdxc/u/transform ]
translateX
Translates the element along the inline axis using the spacing scale or a raw CSS length.
import { translateX } from "@sdxc/u/transform";Quick reference
Every documented call, beside the CSS it emits.
| Call | CSS |
|---|---|
u.translateX(4) | css({ "--ui-translate-x": "calc(var(--ui-spacing, 0.25rem) * 4)", transform: "translate(var(--ui-translate-x, 0), var(--ui-translate-y, 0)) ..." }) |
Examples
Basic example
Use translateX(n) with a step of the spacing scale, like translateX(2) or translateX(6), to move an element horizontally:
<div mix={[...]}>
<div mix={[...]}>
<div mix={[translateX(2), ...]}>translateX(2)</div>
</div>
<div mix={[...]}>
<div mix={[translateX(6), ...]}>translateX(6)</div>
</div>
</div>Using negative values
Pass a negative step, like translateX(-6), to move an element left:
<div mix={[...]}>
<div mix={[...]}>
<div mix={[translateX(-2), ...]}>translateX(-2)</div>
</div>
<div mix={[...]}>
<div mix={[translateX(-6), ...]}>translateX(-6)</div>
</div>
</div>Using percentages and lengths
Pass a string for any CSS length; a percentage like translateX("50%") is a share of the element's own width:
<div mix={[...]}>
<div mix={[...]}>
<div mix={[translateX("50%"), ...]}>translateX("50%")</div>
</div>
<div mix={[...]}>
<div mix={[translateX("12px"), ...]}>translateX("12px")</div>
</div>
</div>Applying on hover
Combine it with hover() to nudge part of a button forward. Hover the button to try it:
<button type="button" mix={[hover(when("& span", translateX(2))), ...]}>
Read more <span mix={[inlineBlock(), transition("transform")]}>→</span>
</button>Using a custom value
Every scale argument also takes a raw CSS value, which passes through untouched.
u.translateX("2.75rem")Applying on a state
Wrap the call in a state utility to scope it to one selector.
u.hover(u.translateX(4))Responsive design
Wrap the call in a container query to scope it to one width.
u.at("md", u.translateX(4))Customizing your theme
Customizing spacing
Every numeric step multiplies --ui-spacing, so redefining it rescales every spacing and sizing utility at once:
:root {
--ui-spacing: 0.25rem;
}Every call that names the step now reads the new value:
translateX(4)Signature
u.translateX(value: SpacingValue)