View
A box laid out with flexbox, the building block of a plugin UI.
A View is a box: it lays its children out with flexbox, paints a
background and a border, clips, and receives events. It is a column by
default, as in React Native.
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.tsxSource- Layout:
flexDirection,justifyContent,alignItems,gap,flex, sizes, margins and padding: see Style. - Focus:
focusablemakes it take keyboard focus when pressed or tabbed to; then it receives key events (Events). WithtabStop={false}as well, Tab skips it while a press andfocus()still focus it: the items of a list or a toolbar that the arrow keys move between (Roving focus). - To assistive technology a view is not an element itself; what it
contains is, unless it is given a role or
accessible(Accessibility).
Scrolling
Set overflowY: 'scroll' on a view to scroll its children vertically,
overflowX: 'scroll' for horizontal scrolling, or overflow: 'scroll'
for both. It clips its content and scrolls by wheel or trackpad, by its
scrollbar, or from code. Animated.View scrolls with the same styles.
import { Pressable, Text, View } from '@soundor/react';
import { useRef, useState } from 'react';
import type { UiNode } from 'soundor:ui';
const PRESETS = Array.from({ length: 24 }, (_, index) => `Preset ${index + 1}`);
// A view with overflowY: 'scroll' scrolls by wheel, trackpad or scrollTo().
export default function PresetList() {
const list = useRef<UiNode>(null);
const [chosen, setChosen] = useState(PRESETS[0]);
return (
<View style={{ width: 260, gap: 8 }}>
<View
ref={list}
style={{
height: 150,
borderRadius: 8,
backgroundColor: '#24262c',
overflowY: 'scroll',
padding: 4,
}}
>
{PRESETS.map((preset) => (
<Pressable
key={preset}
onPress={() => setChosen(preset)}
style={({ hovered }) => ({
padding: 6,
borderRadius: 4,
backgroundColor:
preset === chosen
? '#3a7bfd'
: hovered
? '#2a2c33'
: 'transparent',
})}
>
<Text style={{ color: '#e6e7ea' }}>{preset}</Text>
</Pressable>
))}
</View>
<Pressable
onPress={() => list.current?.scrollTo({ top: 0 })}
style={{
alignSelf: 'flex-start',
padding: 6,
borderRadius: 6,
backgroundColor: '#2a2c33',
}}
>
<Text style={{ color: '#e6e7ea' }}>Back to top</Text>
</Pressable>
</View>
);
}scrolling.tsxSource- Size: give the scrolling view a height or a
flexthat gives it one. Style its children directly; when the content needs a box of its own, put it in anotherViewinside. - Axes:
overflowXandoverflowYwin overoverflow, whatever the order. Set an axis to'hidden'to clip without scrolling it. See Overflow. onScrollfires as it scrolls, by any means; read the node'sscrollTopandscrollLeft. Scrolled from code, it fires once on the next frame, rather than during the call.- Accessibility: a scrolling view is not an element itself; what it
contains is. Given a label or
accessible, it is exposed as a scroll view unless given another role. - Indicators are drawn over the content, taking no space. A mouse near the edge shows a track under a grown bar: the bar drags, and a press on the track jumps there. That 12 pixel strip takes the mouse from what is under it, so leave room with padding if something important is at that edge.
scrollIndicators={false}leaves the indicators out. It scrolls the same; for a scrollbar of your own, seeuseScrollbar.
Scrolling from code
The node, from ref, scrolls as an element does on the Web:
list.current?.scrollTo({ top: 0, behavior: 'smooth' });
list.current?.scrollBy({ top: 100 });
row.current?.scrollIntoView({ block: 'nearest', behavior: 'smooth' });scrollTo()andscrollBy()take{ top, left, behavior }, or(left, top). They move the axes the style scrolls, clamped to the content, and nothing on a node that does not scroll.behavior: 'smooth'takes 300 ms, eased in and out, the same in both runtimes, ascrollevent each frame. A wheel, a scrollbar, or scrolling the node again stops it where it is. With reduced motion asked for, it is there at once.'auto'(the default) and'instant'jump.scrollIntoView()scrolls every view around the node that scrolls, innermost first, to show it:blockvertically andinlinehorizontally, each'start','center','end'or'nearest'(no more than it takes). It defaults toblock: 'start',inline: 'nearest';falseputs it at the end. It goes by layout boxes, so a transform does not change where it scrolls to.
Props
Prop
Type
A View also takes the event props and the
accessibility props.