Style
Flexbox layout, spacing, text and paint, as in React Native.
Styles follow React Native conventions: flexbox laid out by
Yoga, spacing shorthands, percentages, CSS
colors, rounded corners, opacity and transforms. The
node components take style. Portal.Host also takes
style; Modal takes backdropStyle. Portal and FocusScope have no
style of their own.
import { Pressable, Text, View } from '@soundor/react';
import { useState } from 'react';
const DIRECTIONS = ['column', 'row'] as const;
const JUSTIFY = ['flex-start', 'center', 'space-between'] as const;
// Views lay their children out with flexbox: a column by default, as in
// React Native. Change the direction and the spacing along it.
export default function Layout() {
const [direction, setDirection] =
useState<(typeof DIRECTIONS)[number]>('row');
const [justify, setJustify] = useState<(typeof JUSTIFY)[number]>('center');
return (
<View style={{ gap: 12, alignItems: 'center' }}>
<View
style={{
width: 300,
height: 120,
padding: 8,
gap: 8,
flexDirection: direction,
justifyContent: justify,
borderRadius: 8,
backgroundColor: '#24262c',
}}
>
{['#3a7bfd', '#34c77b', '#f2b13a'].map((color) => (
<View
key={color}
style={{
width: 32,
height: 32,
borderRadius: 6,
backgroundColor: color,
}}
/>
))}
</View>
{[DIRECTIONS, JUSTIFY].map((options, row) => (
<View key={row} style={{ flexDirection: 'row', gap: 6 }}>
{options.map((option) => {
const selected = option === direction || option === justify;
return (
<Pressable
key={option}
accessibilityState={{ selected }}
onPress={() =>
row === 0
? setDirection(option as (typeof DIRECTIONS)[number])
: setJustify(option as (typeof JUSTIFY)[number])
}
style={{
paddingVertical: 4,
paddingHorizontal: 8,
borderRadius: 6,
backgroundColor: selected ? '#3a7bfd' : '#2a2c33',
}}
>
<Text style={{ color: 'white', fontSize: 12 }}>{option}</Text>
</Pressable>
);
})}
</View>
))}
</View>
);
}view.tsxSourceValues
- Lengths are numbers, in logical pixels; CSS pixels (
'120px', kept as the number); or percentages of the parent ('50%'). Sizes, margins, positions andflexBasisalso take'auto'. - Colors are any CSS color:
'#rgb[a]','#rrggbb[aa]',rgb(),hsl(), a name, or'transparent'. - An invalid value throws a
TypeErrornaming the property.
Layout
A view lays out its children with flexbox, as in React Native rather than on the Web:
- A column by default (
flexDirection: 'column'), children stretched across it (alignItems: 'stretch'). - No shrinking unless asked:
flexShrinkis 0 by default.flex: n(n > 0) grows by n, shrinks, and starts from zero. - Spacing:
margin,paddingandborderWidthwith their…Horizontal,…Verticaland per-side forms;gap,rowGapandcolumnGapbetween children. - Position:
position: 'absolute'takes a node out of the flow, placed bytop,right,bottom,left(orinset) in its parent's box. - Stacking:
zIndex(an integer) stacks a node among its siblings, for drawing and hit testing alike; layout keeps the tree's order. - Clipping and scrolling:
overflow(Overflow). - Input:
pointerEventslets the pointer through a node, its children, or both;cursorsets its shape (Cursor). - Pixel grid: every edge is rounded to the nearest device pixel where it
is on the screen, so boxes stay crisp at any display scale, and nodes with
the same style get the same pixels. Boxes centred in one parent (by
alignItems,justifyContent, auto margins, or absolutely positioned in the middle) share one centre exactly: where their sizes differ by an odd number of device pixels, a view's size is rounded the other way, or one device pixel added, to fit it (a 20 px disc in a 26 px ring is 31 device pixels at 150 %, not 30). AnImage,CanvasorSvgthat is a whole number of device pixels keeps its size, so it stays sharp, and may sit half a device pixel from its parent's centre instead. Border and outline widths, and the outline's offset, are whole device pixels, rounded down, at least one, as CSS has them:1is one device pixel at 125 % and 150 %,2is two at 125 % and three at 150 %.
Both runtimes lay a style out the same, with the same Yoga, given the same text sizes; see Where layout differs.
Overflow
overflow says what happens to children that reach past a box:
| Value | Clips | Scrolls |
|---|---|---|
'visible' (default) | no | no |
'hidden' | yes | no |
'scroll' | yes | yes: wheel, scrollbar, scrollTo(), with scroll events |
Any view scrolls with 'scroll', an Animated.View
too (Scrolling). It is the CSS meaning,
not React Native's, where 'scroll' only clips.
<Animated.View
style={{ overflowY: 'scroll', height: 200 }}
transition={{ type: 'spring' }}
>
{rows}
</Animated.View>- One axis:
overflowXandoverflowYtake the same values, and win overoverflowwhatever the order.{ overflowX: 'scroll' }is a strip that scrolls sideways;{ overflow: 'scroll', overflowX: 'hidden' }only scrolls down. - Both clip or neither: a box that scrolls clips both ways, so
'visible'on one axis beside a clipping one clips too, as'hidden', as in CSS. - Style arrays merge key by key, so a later
overflowdoes not undo an earlieroverflowX, unlike a CSS shorthand: set both axes, or onlyoverflow, to change them later. - Only a view's children scroll. On a
Text, an image or a canvas,'scroll'clips, as'hidden'; put long text in a view that scrolls. - Stopping: a view whose style no longer scrolls an axis goes back to its
start there, and fires
scroll. - Scrollbars: a view that scrolls draws thin indicators for the axes it
scrolls, unless its
scrollIndicatorsprop isfalse(useScrollbardraws your own). - From code: the node's
scrollTo(),scrollBy()andscrollIntoView(), smooth withbehavior: 'smooth'(Scrolling from code).
Text
fontFamily, fontSize, fontWeight, fontStyle, lineHeight,
letterSpacing, textAlign and color style a Text
(and a TextInput's text). fontFamily is a
bundled font registered with registerFont, or one installed on the system
(Fonts).
Paint and transforms
backgroundColor, borderColor, borderRadius (all corners, or each),
opacity (for the node and everything in it), and transform: an ordered
list of translateX, translateY, scale, scaleX, scaleY and rotate
(radians as a number, or '90deg'), about the node's centre. A transform
moves how the node is drawn and hit, not its layout;
Animated.View animates it.
willChange: 'transform' asks the renderer to prepare a view for transform
changes. The web runtime passes it to CSS as will-change: transform. The
VST3 runtime can reuse the view's cached pixels while its transform changes.
Use the hint on individual views where it helps, since cached layers take
memory. Omit it or use 'auto' for the default.
Outline
outlineWidth draws a line around a node, as CSS's outline: outside its
border, following its borderRadius, outlineOffset away from it (inside
when negative), in outlineColor (black by default). Unlike a border it
takes no room and is not hit, so it can come and go without moving
anything: a focus ring
(Focus rings).
<View
style={{
borderRadius: 8,
outlineWidth: 2,
outlineOffset: 2,
outlineColor: '#3a7bfd',
}}
/>Leave room for it: an ancestor with overflow: 'hidden' clips it, and in
the VST3 runtime a later sibling drawn over the same place covers it (a
browser draws outlines above).
Shadows
boxShadow is a list of shadows, as CSS's box-shadow, the first drawn on
top. Each is the box's shape grown by spread (shrunk when negative, its
rounded corners growing with it), moved by offsetX and offsetY, and
blurred by blur (a Gaussian with half of it as its standard deviation),
in color (black by default). Every field is optional:
import { Pressable, Text, View, type Style } from '@soundor/react';
import { useState } from 'react';
const label: Style = { color: '#c9ccd4', fontSize: 12 };
// `boxShadow` lists shadows, the first on top: raised outside the box, or
// sunk into it with `inset`. They follow the corners and take no room.
export default function BoxShadows() {
const [armed, setArmed] = useState(false);
// A percentage, in steps of 20: each press raises it, past 100 back to 20.
const [level, setLevel] = useState(60);
return (
<View
style={{
width: 340,
padding: 20,
gap: 20,
borderRadius: 12,
backgroundColor: '#2a2c33',
// Raised: a soft shadow below, and a sharp one close to it.
boxShadow: [
{ offsetY: 8, blur: 24, color: 'rgba(0, 0, 0, 0.45)' },
{ offsetY: 1, blur: 2, color: 'rgba(0, 0, 0, 0.4)' },
],
}}
>
<View style={{ gap: 8 }}>
<Text style={label}>Level {level}%</Text>
{/* Sunk: inset shadows inside the track, light along its bottom. */}
<Pressable
accessibilityLabel="Level"
onPress={() => setLevel(level >= 100 ? 20 : level + 20)}
style={{
height: 14,
borderRadius: 7,
overflow: 'hidden',
backgroundColor: '#1b1c21',
boxShadow: [
{ inset: true, offsetY: 2, blur: 4, color: 'rgba(0, 0, 0, 0.7)' },
{ inset: true, offsetY: -1, color: 'rgba(255, 255, 255, 0.06)' },
],
}}
>
<View
style={{
width: `${level}%`,
height: '100%',
borderRadius: 7,
backgroundColor: '#34c77b',
}}
/>
</Pressable>
</View>
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 16 }}>
{/* A glow is a colored shadow with spread; pressing sinks the knob. */}
<Pressable
accessibilityLabel="Arm"
accessibilityState={{ selected: armed }}
onPress={() => setArmed(!armed)}
style={({ pressed }) => ({
width: 56,
height: 56,
borderRadius: 28,
backgroundColor: '#3a3d46',
boxShadow: [
...(armed
? [{ spread: 3, blur: 12, color: 'rgba(58, 123, 253, 0.8)' }]
: []),
pressed
? {
inset: true,
offsetY: 2,
blur: 6,
color: 'rgba(0, 0, 0, 0.6)',
}
: { offsetY: 4, blur: 8, color: 'rgba(0, 0, 0, 0.5)' },
],
})}
/>
<Text style={label}>{armed ? 'Armed' : 'Press to arm'}</Text>
</View>
</View>
);
}box-shadow.tsxSource<View
style={{
borderRadius: 12,
backgroundColor: '#2a2c33',
boxShadow: [
{ offsetY: 8, blur: 24, color: 'rgba(0, 0, 0, 0.45)' },
{ offsetY: 1, blur: 2, color: 'rgba(0, 0, 0, 0.4)' },
],
}}
/>- Outside the box by default: only around it, never under it, so a translucent background does not show its shadow through.
- Inside with
inset: true: within the border, over the background and under what the node holds, as a sunk well or a pressed button. - Like an outline, a shadow takes no room and is not hit. An ancestor with
overflow: 'hidden'clips it, so leave room for a large blur.
Animated.View does not animate shadows; set another boxShadow to change
one, as the knob above does when pressed.
Cursor
import { Text, View, type Style } from '@soundor/react';
import { useRef, useState } from 'react';
const CURSORS = [
'default',
'pointer',
'text',
'crosshair',
'move',
'none',
'ew-resize',
'ns-resize',
'nesw-resize',
'nwse-resize',
] as const;
const tile: Style = {
width: 92,
paddingVertical: 6,
alignItems: 'center',
borderRadius: 6,
backgroundColor: '#2a2c33',
};
const label: Style = { color: 'white', fontSize: 12 };
// `cursor` sets the pointer's shape over a node and its children. Hover the
// tiles; drag the fader and leave it: its cursor stays until you release.
export default function Cursors() {
const [level, setLevel] = useState(0.6);
const drag = useRef<{ x: number; level: number } | null>(null);
return (
<View style={{ width: 484, gap: 10 }}>
<View style={{ flexDirection: 'row', flexWrap: 'wrap', gap: 6 }}>
{CURSORS.map((cursor) => (
<View key={cursor} style={[tile, { cursor }]}>
<Text style={label}>{cursor}</Text>
</View>
))}
</View>
{/* Children take their parent's cursor unless they set their own. */}
<View
style={{
flexDirection: 'row',
gap: 6,
padding: 6,
borderRadius: 8,
backgroundColor: '#24262c',
cursor: 'crosshair',
}}
>
<View style={[tile, { flex: 1 }]}>
<Text style={label}>inherits crosshair</Text>
</View>
<View style={[tile, { flex: 1, cursor: 'pointer' }]}>
<Text style={label}>its own: pointer</Text>
</View>
</View>
<View
onPointerDown={(event) => {
drag.current = { x: event.pageX, level };
}}
onPointerMove={(event) => {
if (drag.current === null) return;
const moved = (event.pageX - drag.current.x) / 484;
setLevel(Math.min(1, Math.max(0, drag.current.level + moved)));
}}
onPointerUp={() => {
drag.current = null;
}}
onPointerCancel={() => {
drag.current = null;
}}
style={{
height: 28,
justifyContent: 'center',
borderRadius: 6,
overflow: 'hidden',
backgroundColor: '#2a2c33',
cursor: 'ew-resize',
}}
>
<View
style={{
position: 'absolute',
left: 0,
top: 0,
bottom: 0,
width: `${level * 100}%`,
backgroundColor: '#3a7bfd',
}}
/>
<Text style={[label, { paddingHorizontal: 8 }]}>
Drag: {Math.round(level * 100)}%
</Text>
</View>
</View>
);
}cursor.tsxSourcecursor sets the pointer's shape over a node and everything in it, until a
child sets its own. It is 'auto' by default, which takes the parent's: the
arrow ('default') at the root, whatever is drawn there, and the I-beam
('text') in a TextInput. No component sets
one for you, buttons included, as in desktop apps and plugins; add
cursor: 'pointer' where you want the hand.
<View style={{ cursor: 'ns-resize' }} onPointerDown={startDrag} />While a pointer is pressed, the pressed node's cursor shows wherever it is
dragged, so a knob's stays on until release. 'none' hides the pointer. The
shapes are those every platform has: 'default', 'none', 'pointer',
'text', 'crosshair', 'move', 'ew-resize', 'ns-resize',
'nesw-resize' and 'nwse-resize'.
Combining styles
style takes arrays and falsy entries, merged in order (later wins), so a
condition can add a style:
import { StyleSheet, View } from '@soundor/react';
const styles = StyleSheet.create({
knob: { width: 48, height: 48, borderRadius: 24 },
active: { backgroundColor: '#3a7bfd' },
});
<View style={[styles.knob, active && styles.active]} />;StyleSheet.create() returns its styles as they are, typed, and
StyleSheet.flatten() merges a style prop into one object.
Every property
Prop
Type