On This Page
Creating Components
Semantic UI extends native web components to support reactive data, event binding, templating, DOM Querying, keybindings and more to allow you to build complex web applications purely with web components.
Installing Dependencies
The only package necessary to create components is the @semantic-ui/component
package.
Creating Components
defineComponent
exported from @semantic-ui/component
can be used to create a new web component.
Minimal Component
The minimum requirement to create a component is an html template.
If you specify a tagName
your component will be available as a web component allowing you include it in pages in your application.
Standard Component
In most real world use-cases you will need to provide additional information to defineComponent
- createComponent - An object returned from
createComponent
defines what methods and properties are attached to the DOM element associated with the component which provide behaviors. For instance a popup might have ashow
andhide
behavior. - Template - The logic that controls how your html is rendered including conditionals, loops, subtemplates, and expressions. This can be passed in as a string
template
or rendered from a precompiledast
. - CSS - The
css
scoped to this component or that should be attached once to the page with the component, - Page CSS - The
pageCSS
scoped to the page that will added to the page when a component is defined. - Events - The event listeners attached to your component, passed in as an object literal string
events
. - State - Reactive internal
state
associated with your component and its default values - Settings - Reactive external
settings
exposed to users as properties or attributes on yourtagName
- Lifecycle Events - Actions that should occur
onCreated
,onRendered
andonDestroyed
for your component - Keybindings -
keys
defines keyboard events associated with your component
Destructured Params - Semantic callbacks provide a variety of parameters that are returned from functions called inside the component. For more information see callback arguments
Component Parts
Instance
createComponent
defines the behaviors associated with your component. These can be called from your template, other components, or using javascript in your page.
Your components state
, settings
, component instance self
and other data sources are passed through to each lifecycle arguments like createComponent
.
For more information on
createComponent
see our dedicated subsection on defining functionality.
Template
The template
defines the html your component should render from the data present in your component.
For more information on templating see the dedicated subsection for templating
CSS
css
defines scoped CSS that will be attached to your web component and not apply to other elements on the page. This is passed as a string and is constructed and attached to the component using adoptedstylesheets.
You can use pageCSS
to add additional css that will be included once in the page scope when you component is created.
For more information see the dedicated css styling section.
Events
Most components will need to listen to and react to events triggered from the browser or other components. When creating a component you can assign events
that are bound to parts of your component and trigger functionality.
These can be browser events like click
, perhaps to save something when a button with class save
is clicked.
For more information on events please see the dedicated subsection for events
or custom events like resizestart
dispatched from another component
Keys
Components can also include keybindings that will be attached to the page during the lifecycle of the component.
For more information please see the dedicated subsection for keybindings
Organizing Code
To keep your code organized you might consider keeping your html
and css
as separate files with your component.
Browser / Node
In a browser or node
you can import static assets using getText
a helper that uses fetch
.
Webpack / Vite / Esbuild etc.
In a project with build tools like you can use the ?raw
query parameter to import a plaintext file directly.
Related Docs
- Vite - Import Assets as a String
- Webpack - Raw Loader
Single File Components
If you prefer single file components you can just inline your html and css.