LIB/UI is a collection of Svelte 5 components meant to be downloaded directly to src/lib/ui
in
your project. Since these are just files sitting in a directory and not hidden away in your Node
modules, that means you can directly modify the components to fit your needs. This keeps things
simple and transparent.
LIB/UI isn’t a Node package so it isn’t installed. Just download the zip
file to your SvelteKit project’s src/lib/
and extract it.
The only dependency requirement besides SvelteKit is SuperForms which is used for form validation in
the <Form>
component.
npm install sveltekit-superforms
LIB/UI components are imported and instantiated like any other Svelte component.
<script>
import { Spinner } from '$lib/ui';
</script>
<Spinner />
Properties are used to pass options to the component. For example, the Alert
component takes an
optional scheme
property which can be used to set the color/icon theme of the alert.
<Alert scheme="danger">Something assploded.</Alert>
Slots are used to pass default content as seen in the above example. However, many LIB/UI components
also use named slots to pass additional content to specific areas of the component. For
example, the Alert
component accepts an optional title
slot which can be used to set a title to
be displayed at the top of the alert.
<Alert scheme="success">
<div slot="title">Hooray!</div>
This is a total success.
</Alert>
CSS custom properties are used to set colors, spacing, and other values used by LIB/UI components. For example, to change the default border radius used for things like form elements and alerts, you could add this to your project’s CSS:
:root {
--ui-border-radius: 4px;
}
This approach has a number of advantages over using regular Svelte props because of the way CSS rules cascade and can be overridden. For example, you can set a global border radius for all form elements, but then override it for a specific form element if needed. Or you can even create classes for your own alternate versions of LIB/UI components that are styled differently from the rest.
There are also CSS custom properties available for individual components. For example, the Alert
component will use the global --ui-border-radius
value by default. But you can change the default border
radius for only Alert components like this:
:root {
--ui-alert-border-radius: 8px;
}
You can even set custom properties directly on a single component instance in your Svelte file:
<Alert --ui-alert-border-radius="8px">
This alert has a custom border radius.
</Alert>
This is made possible by a very handy Svelte Component
Directive called --style-props
which
allows you to set custom properties directly within the scope of a component instance with the
convenient shorthand syntax shown above.
If you need to create an alternate style for a component such as making a dark version of a Form,
this can be easily done with a simple CSS class. All LIB/UI components have a lib-ui
class and a
class that matches the component’s name. So to create a dark version of a Form, you could create
a class like this:
.lib-ui.Form.dark {
--ui-input-background: #333;
--ui-input-color: #fff;
--ui-input-label-color: #fff;
--ui-button-background: #444;
--ui-button-color: #fff;
}
Then by adding the dark
class to a Form component, these styles will be set for the Form element
and all of its child components.
<Form class="dark">
<Input label="name" />
<Button>Submit</Button>
</Form>
There are many global custom properties that can be set to change the default appearance of LIB/UI components. They are all optional and will fall back to default values if not set.
Name | Type | Default | Description |
---|---|---|---|
GLOBAL COLORS | |||
--ui-focus | Color | #3b82f6 | Accent color used for things like buttons and links |
--ui-dark | Color | #1e293b | Dark color used for things like text |
--ui-midtone | Color | #94a3b8 | Midtone color used for things like borders |
--ui-light | Color | #e2e8f0 | Light color used for things like disabled form element background |
--ui-info | Color | #1e40af | Dark color used for things like text in components with scheme value 'info' |
--ui-warning | Color | #9a3412 | Dark color used for things like text in components with scheme value 'warning' |
--ui-success | Color | #166534 | Dark color used for things like text in components with scheme value 'success' |
--ui-error | Color | #b91c1c | Dark color used for things like text in components with scheme value 'danger' |
--ui-info-bg | Color | #dbeafe | Light color used for things like Alert background scheme value 'info' |
--ui-warning-bg | Color | #fef08a | Light color used for things like text in components with scheme value 'warning' |
--ui-success-bg | Color | #bbf7d0 | Light color used for things like text in components with scheme value 'success' |
--ui-error-bg | Color | #fee2e2 | Light color used for things like text in components with scheme value 'danger' |
GLOBAL BORDERS AND OUTLINES | |||
--ui-border-width | Numeric | 3px | Border width for things like form elements |
--ui-border-radius | Numeric | 3px | Border radius for things like form elements |
--ui-border-color | Color | --ui-midtone , #94a3b8 | Border color for things like form elements |
--ui-outline-width | Numeric | 1px | Outline width for things like focused or error form elements |
--ui-outline-offset | Numeric | 3px | Outline offset for things like focused or error form elements |
GLOBAL GLOBAL SPACING AND TYPOGRAPHY | |||
--ui-margin | Numeric | 1rem | Sets margin-top on form elements within a form |
--ui-padding | CSS Padding Shorthand | 4px 6px | Padding for form fields |
--ui-font-size | Numeric | 0.875rem | Font size for things like Input text |
--ui-small-font-size | Numeric | 0.875rem | Font size for things like checkbox labels |
--ui-alert-color
are documented in each
component's documentation.You can also pass a class
property to any LIB/UI component to add additional classes to the
component’s outermost element. This can be especially powerful when used with Tailwind utility
classes – and of course, classes can also be used directly on named slots as usual. So for an Alert
which has a fixed width and drop shadow and a title that’s underlined, you could do something like
this:
<Alert scheme="success" class="w-96 shadow-xl">
<div slot="title" class="underline">Hooray!</div>
Item added to cart.
</Alert>
Component files are well organized and commented to make it easy to understand what’s going on. You
can modify the components as much or as little as you like. You can also delete the ones you don’t
need. Be sure to also remove the reference from lib/ui/index.js
.
Using one of the existing components as a template, you can also create your own components and add
them to the lib/ui
directory. Remember to also add a line to export your custom component from lib/ui/index.js
.