Components
Write your pages, panels, status items and overlays in JSX, from Songstress's own components, which every app draws in its own look.
A page, a panel, a status item or an overlay is a tree of Songstress's own parts
(Pages, panels and status). songstress/ui lets you write that tree as JSX. Each component
emits one node, its props the node's, and every app draws it with its own controls: the web and desktop apps in their
design, the phones natively, in whatever theme the person picked. You never choose a colour, a font or a pixel, except
on a canvas of your own.
In client code a component keeps state while the person has it open, and a press calls your function in the extension's sandbox, beside the app: see State and handlers.
Set up
npm create songstress-extension@latest -- --template components starts an extension with a page written this way,
and its test. In an extension you have, tell the compiler where JSX comes from:
{ "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "songstress/ui" } }Your editor, tsc and Vitest read it, and songstress build compiles JSX the same way. Write the part in a .tsx
file, import it from its entry as any other part, and ask for "songstress": "^0.1.12" in engines: the components
came with it.
import { songstress } from 'songstress/client'
import { Button, HStack, Section, Stack, Text } from 'songstress/ui'
songstress.ui.pages.register('greeting', () => (
<Stack gap="medium">
<Section title="Tonight">
<Text style="title">Hello</Text>
<Text tone="muted">Pick something to listen to.</Text>
</Section>
<HStack gap="small">
<Button title="Say hello" icon="sparkles" emphasis="primary" onPress={{ type: 'toast', message: 'Hello' }} />
</HStack>
</Stack>
))The page is declared as any other, with "render": "client" (or "server": a server renders the same JSX once, see
On the server).
The components
Each one, as Songstress draws it, with its props and a compiled example:
| For | Components |
|---|---|
| Layout | Stack, HStack (side by side), Grid, Box (plain, the app's card, a quiet inset), Section, Spacer, Divider, Collapsible, Tabs with Tab |
| Text and media | Text (title, heading, body, caption, label, code), ProseText, Glyph (an icon, by name), Image, Sprite, Cover, Badge, Progress, Callout (a banner across the top of a block), Empty, Skeleton |
| Controls | Button, IconButton, Menu with MenuOption, Switch, Checkbox, Slider, Select, Segmented, TextInput, SearchField, DatePicker |
| Collections | List with ListItem, Table, Tree with TreeItem, Shelf with MediaCard |
| The library | SongItem, AlbumItem, ArtistItem, PlaylistItem (by library id, drawn as Songstress draws them, with their own menus), PlayButton |
| Pixels of your own | Canvas |
Room is a step (none, xsmall, small, medium, large, xlarge), colour a meaning (tone: default,
muted, accent, success, warning, danger, info), and a button's weight an emphasis (primary,
secondary, quiet, danger). Every component also takes key (its name among its siblings, so it keeps its place
and what was typed into it), grow (it takes the room left in an HStack) and width (fill or hug).
A component of your own is a function of props that answers these. A node you write by hand may sit inside an element and is kept as written.
The library's components take a library id. Client code learns ids from the player (usePlayer,
songstress.queue.get), from a panel's context (the song, album, artist or playlist of the page the panel stands on),
from the library's lists (songstress.library.listSongs, from API 0.1.14), or from a command of your server entry.
Your package's own images
An Image whose source is { asset: "assets/mark.png" }, and every Sprite, shows a file your package carries.
Declare each in the manifest with its real size, a PNG, JPEG or WebP of 1 MiB at most, and ask for API 0.1.12:
"engines": { "songstress": "^0.1.12" },
"contributes": {
"assets": [{ "path": "assets/pet.webp", "width": 1536, "height": 1872 }]
}songstress pack carries them, and the server serves each to the apps by its digest. Each is a still image (an
animated PNG or WebP is refused, since it would play under reduced motion), and all of a package's images hold 4,096 by
4,096 pixels in all at most. A path the manifest does not declare shows nothing, and songstress check warns of a file
that is not the size it says or is animated (ASSET_IMAGE), or of a file or a set of images too large
(ASSET_TOO_LARGE), which the server would refuse to install. A member's own picture, one they gave your extension,
is a file of storage.files ({ file: 'pets/prince.webp' }), API 0.1.14: see Data and the app.
What a component can't do
What a tree can't do, a component can't either: run script in the app, choose a colour, a font, a size in pixels or a position, load an image from the device, or grow past the caps. Its code runs in your extension's sandbox, beside the app, never in it.
Test it
songstress/testing mounts a component as an app does: host.mount(kind, id) for what your entry registered, then
press, change and submit what the tree names. See State and handlers and
Test and debug.