README
Svelte Material UI - Common Components
Common Label and Icon components, elemental components, and helper utilities.
You can use the elemental components to switch the HTML element that an SMUI component with a component
prop uses.
Installation
npm install --save-dev @smui/common
Examples and Usage Information
https://sveltematerialui.com/demo/common
Exports
Label
A common label.
The common label is used everywhere that exports a Label
component.
Props / Defaults
component
:Span
- A component to use as the root element.use
:[]
- An array of Svelte actions and/or arrays of an action and its options.class
:''
- A CSS class string.
Icon
A common icon.
The common icon is used everywhere that exports an Icon
component except for textfield
and select
.
Props / Defaults
component
:I
- A component to use as the root element.use
:[]
- An array of Svelte actions and/or arrays of an action and its options.class
:''
- A CSS class string.on
:false
- Used in the context of an icon button toggle to denote the icon for when the button is on.
Elemental Components
These components are used in SMUI components that take a component
prop. They determine which HTML element will be used as the root element of the component. You can import them from @smui/common/elements
.
Props / Defaults (Common to all Elemental Components)
use
:[]
- An array of Svelte actions and/or arrays of an action and its options.
A
An elemental component for the a
tag.
Aside
An elemental component for the aside
tag.
Button
An elemental component for the button
tag.
Div
An elemental component for the div
tag.
Em
An elemental component for the em
tag.
Footer
An elemental component for the footer
tag.
H1
An elemental component for the h1
tag.
H2
An elemental component for the h2
tag.
H3
An elemental component for the h3
tag.
H4
An elemental component for the h4
tag.
H5
An elemental component for the h5
tag.
H6
An elemental component for the h6
tag.
Header
An elemental component for the header
tag.
Hr
An elemental component for the hr
tag.
I
An elemental component for the i
tag.
Img
An elemental component for the img
tag.
Label
An elemental component for the label
tag.
Li
An elemental component for the li
tag.
Main
An elemental component for the main
tag.
Nav
An elemental component for the nav
tag.
P
An elemental component for the p
tag.
Section
An elemental component for the section
tag.
Span
An elemental component for the span
tag.
Strong
An elemental component for the strong
tag.
Svg
An elemental component for the svg
tag.
Ul
An elemental component for the ul
tag.
Helper Utilities
Helper utilities are exported from the @smui/common/internal
endpoint. They are used within SMUI to provide additional functionality outside of the features the Svelte API is natively capable of. You can use them in your own components to provide the same additional functionality.
classAdderBuilder
and forwardEventsBuilder
use internal Svelte features. Since they depend on svelte/internal
, you should consider use of them the same way you consider use of svelte/internal
directly.
classMap
Build a class string from a map of class names to conditions. This is useful when you need to add classes to a component, since Svelte's "class:" directives don't work on components. (It's also useful for actions that take addClass
and removeClass
functions.)
<SomeComponent
class={classMap({
'my-always-on-class': true,
'my-conditional-class': condition,
...internalClasses,
})}
>
I've got class.
</SomeComponent>
<script lang="ts">
import SomeComponent from './SomeComponent.svelte';
export let condition = true;
let internalClasses: { [k: string]: boolean } = {};
export function addClass(className: string) {
if (!internalClasses[className]) {
internalClasses[className] = true;
}
}
export function removeClass(className: string) {
if (!(className in internalClasses) || internalClasses[className]) {
internalClasses[className] = false;
}
}
</script>
dispatch
Dispatch a custom event. This differs from Svelte's component event system, because these events require a DOM element as a target, can bubble (and do by default), and are cancelable with event.preventDefault()
. All SMUI events are dispatched with this instead of Svelte's createEventDispatcher
.
<div
bind:this={eventTarget}
on:mouseover={emitEvent}
on:click={emitCancelableEvent}
tabindex={0}
/>
<script lang="ts">
import { dispatch } from '@smui/common/internal';
let eventTarget;
function emitEvent(originalEvent: Event) {
dispatch(eventTarget, 'MyCustomEvent', { originalEvent });
// You would access originalEvent with `event.detail.originalEvent`.
}
function emitCancelableEvent(originalEvent: Event) {
const event = dispatch(
eventTarget,
'MyCustomEvent',
{ originalEvent },
{
bubbles: true,
cancelable: true,
}
);
if (!event.defaultPrevented) {
alert('The event was not canceled!');
}
}
</script>
exclude
Exclude a set of properties from an object. It differs from normal omit
functions by also excluding all properties that begin with a given string if that string ends with "