Components › Components
Demos show your theme.
Toast
Short messages any island can send, announced politely, gone after a few seconds.
Deleted: 0
Usage #
The parts come from @clarusjs/ui-primitives; the look is toast.css, imported once.
tscl
{{-- Toasts of each kind. Your own look: give the region templates (one root element each: it becomes the toast),
and it copies them for each toast.
<toastRegion>
@slot('toast') {
<div class="my-toast">
<span data-part="toast-icon"></span>
<p data-part="toast-message"></p>
<button type="button" data-part="toast-action"></button>
<button type="button" data-part="toast-close" aria-label="Dismiss">×</button>
</div>
}
@slot('error') { … the same parts, another look for errors … }
</toastRegion>
--}}
@import './toast.css'
@import uiButton from './button.tscl'
<script>
import { toast } from '@clarusjs/ui-primitives'
let removed = state(0)
const save = () => toast('Saved', { type: 'success' })
const fail = () => toast('Upload failed', { type: 'error' })
const note = () => toast('A new version is ready', { type: 'info' })
const warn = () => toast('Storage almost full', { type: 'warning' })
function remove() {
removed++
toast('Project deleted', { action: { label: 'Undo', onClick: () => removed-- } })
}
</script>
<div class="demo">
<uiButton (click)="save()">Save</uiButton>
<uiButton variant="outline" (click)="fail()">Fail</uiButton>
<uiButton variant="outline" (click)="note()">Info</uiButton>
<uiButton variant="outline" (click)="warn()">Warn</uiButton>
<uiButton variant="outline" (click)="remove()">Delete project</uiButton>
<p>
Deleted: <span class="out">{{ removed }}</span>
</p>
</div>
<style scoped>
:scope {
display: flex;
flex-wrap: wrap;
gap: 12px;
align-items: center;
padding: 24px;
border: 1px solid var(--ui-border);
border-radius: var(--ui-radius);
background: var(--ui-bg);
color: var(--ui-fg);
}
p {
margin: 0;
}
</style>Source #
Copy it next to your components as toast.css. It's the same as @clarusjs/ui's rules for these parts, so using both looks the same.
css
/*
Toast: the same rules as @clarusjs/ui's ui.css for toast-region.tscl from @clarusjs/ui-primitives (and the
messages toast() adds). Copy it next to your components, @import it once.
*/
@layer theme, base, components, utilities;
@layer components {
/* Focus is always visible, on every part and what's inside it. */
:where([data-part]):focus-visible,
:where([data-part]) :focus-visible {
outline: 2px solid var(--ui-ring);
outline-offset: 2px;
}
/* Toast: messages stacked at the bottom-right corner, above the page; each a card with its action and a close. */
:where([data-part='toast-region']) {
position: fixed;
inset: auto 16px 16px auto;
z-index: 50;
width: min(22rem, calc(100vw - 32px));
}
:where([data-part='toast-list']) {
display: grid;
gap: 8px;
margin: 0;
padding: 0;
list-style: none;
}
:where([data-part='toast']) {
position: relative;
display: flex;
gap: 8px;
align-items: center;
padding: 12px 14px;
border: 1px solid var(--ui-border);
border-radius: var(--ui-radius);
background: var(--ui-popover);
color: var(--ui-popover-fg);
box-shadow: var(--ui-shadow);
font: var(--ui-font-size)/1.4 var(--ui-font);
}
:where([data-part='toast-message']) {
flex: 1;
margin: 0;
}
:where([data-part='toast-action']),
:where([data-part='toast-close']) {
padding: 4px 8px;
border: 1px solid var(--ui-border);
border-radius: calc(var(--ui-radius) - 2px);
background: transparent;
color: inherit;
font: inherit;
cursor: pointer;
}
:where([data-part='toast-close']) {
border-color: transparent;
}
:where([data-part='toast'])[data-type='success'] {
border-left: 4px solid var(--ui-success);
}
:where([data-part='toast'])[data-type='success'] :where([data-part='toast-icon']) {
color: var(--ui-success);
}
:where([data-part='toast'])[data-type='error'] {
border-left: 4px solid var(--ui-danger);
}
:where([data-part='toast'])[data-type='error'] :where([data-part='toast-icon']) {
color: var(--ui-danger);
}
:where([data-part='toast'])[data-type='warning'] {
border-left: 4px solid var(--ui-warning);
}
:where([data-part='toast'])[data-type='warning'] :where([data-part='toast-icon']) {
color: var(--ui-warning);
}
:where([data-part='toast'])[data-type='info'] {
border-left: 4px solid var(--ui-accent);
}
:where([data-part='toast'])[data-type='info'] :where([data-part='toast-icon']) {
color: var(--ui-accent);
}
:where([data-part='toast-icon']) {
display: inline-flex;
flex-shrink: 0;
}
:where([data-part='toast-icon']):empty {
display: none;
}
:where([data-part='toast-kind']) {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
@media (prefers-reduced-motion: reduce) {
:where([data-part]),
:where([data-part]) * {
transition: none !important;
}
}
}Styling #
- On the parts:
class,style,id,aria-*anddata-*written on a part's tag (from@clarusjs/ui-primitives) land on its root element, merged with its own. - Selectors:
data-partnamestoast-region,toast-list,toast,toast-message,toast-action,toast-close,toast-icon,toast-kind. - Your rules win: every rule here has zero specificity (
:where()) and sits in@layer components, so your own CSS and Tailwind utilities override it.
Needs #
- theme.css, once per project
-
@clarusjs/ui-primitives≥ 0.2.0: toast-region.tscl