
Enter a search term above to see results...
Enter a search term above to see results...
Settings are values which you want users of your component to be able to modify. They act as the public API for your component, allowing developers to configure it for different use cases.
To specify settings for a component, pass in a settings object when defining a component:
const defaultSettings = { name: 'Jack', color: 'blue', size: 'medium', count: 5};
defineComponent({ tagName: 'name-card', defaultSettings});
A setting’s type is inferred from its default value, so you don’t need to explicitly declare types.
Settings represent the external API for your component, while state represents the internal configuration of your component.
For example, in a dropdown component:
Your component can have its default settings modified by an end user in several different ways.
The most common way to override settings is through HTML attributes:
<name-card name="Sally" color="red" size="large" count="10"></name-card>
You can also set settings directly by modifying the properties of a component
const el = document.querySelector('name-card');el.name = 'Sam';el.color = 'green';
You can use Query to access your component and update settings.
Use the settings()
method to set multiple properties on one or more components:
import { $ } from '@semantic-ui/query';
// Configure a single component$('name-card').settings({ name: 'Alex', color: 'purple'});
// Configure multiple components at once$('.user-cards name-card').settings({ showAvatar: true, displayMode: 'compact'});
Use the setting()
method to set a single property on one or more components:
import { $ } from '@semantic-ui/query';
// Set a single property$('name-card').setting('name', 'Alex');
// Set a function property$('user-form').setting('onSubmit', (data) => { saveUserData(data);});
For components that need to be configured before they’re used, the initialize()
method sets properties after the DOM is fully loaded:
import { $ } from '@semantic-ui/query';
// Wait for DOM to be ready, then set properties$('name-card').initialize({ onSelect: (value) => console.log('Selected:', value), formatter: (text) => text.toUpperCase()});
This is especially useful for passing function references that can’t be serialized as HTML attributes.
Different data types require different handling when passed as settings:
Pass strings and numbers directly through attributes or properties:
<price-display amount="99.99" currency="USD"></price-display>
When passing complex types through attributes, serialize them with JSON:
<user-list users='[{"name":"Alex","role":"admin"},{"name":"Sam","role":"user"}]'></user-list>
When using JavaScript properties, you can pass the values directly:
const userList = $('user-list').setting(users, [ { name: 'Alex', role: 'admin' }, { name: 'Sam', role: 'user' }]);
Functions can only be passed through JavaScript:
$('user-form').setting('onSubmit', (data) => { saveUserData(data);};