Tag
<cw-tag> | CwTag
Tags are used as labels to organize things or to indicate a selection.
<cw-tag variant="primary">Primary</cw-tag> <cw-tag variant="success">Success</cw-tag> <cw-tag variant="neutral">Neutral</cw-tag> <cw-tag variant="warning">Warning</cw-tag> <cw-tag variant="danger">Danger</cw-tag>
import CwTag from '@cordwainer/cw-elements/dist/react/tag'; const App = () => ( <> <CwTag variant="primary">Primary</CwTag> <CwTag variant="success">Success</CwTag> <CwTag variant="neutral">Neutral</CwTag> <CwTag variant="warning">Warning</CwTag> <CwTag variant="danger">Danger</CwTag> </> );
Examples
Sizes
Use the size attribute to change a tag’s size.
<cw-tag size="small">Small</cw-tag> <cw-tag size="medium">Medium</cw-tag> <cw-tag size="large">Large</cw-tag>
import CwTag from '@cordwainer/cw-elements/dist/react/tag'; const App = () => ( <> <CwTag size="small">Small</CwTag> <CwTag size="medium">Medium</CwTag> <CwTag size="large">Large</CwTag> </> );
Pill
Use the pill attribute to give tags rounded edges.
<cw-tag size="small" pill>Small</cw-tag> <cw-tag size="medium" pill>Medium</cw-tag> <cw-tag size="large" pill>Large</cw-tag>
import CwTag from '@cordwainer/cw-elements/dist/react/tag'; const App = () => ( <> <CwTag size="small" pill> Small </CwTag> <CwTag size="medium" pill> Medium </CwTag> <CwTag size="large" pill> Large </CwTag> </> );
Removable
Use the removable attribute to add a remove button to the tag.
<div class="tags-removable"> <cw-tag size="small" removable>Small</cw-tag> <cw-tag size="medium" removable>Medium</cw-tag> <cw-tag size="large" removable>Large</cw-tag> </div> <script> const div = document.querySelector('.tags-removable'); div.addEventListener('cw-remove', event => { const tag = event.target; tag.style.opacity = '0'; setTimeout(() => (tag.style.opacity = '1'), 2000); }); </script> <style> .tags-removable cw-tag { transition: var(--cw-transition-medium) opacity; } </style>
import CwTag from '@cordwainer/cw-elements/dist/react/tag'; const css = ` .tags-removable cw-tag { transition: var(--cw-transition-medium) opacity; } `; const App = () => { function handleRemove(event) { const tag = event.target; tag.style.opacity = '0'; setTimeout(() => (tag.style.opacity = '1'), 2000); } return ( <> <div className="tags-removable"> <CwTag size="small" removable onCwRemove={handleRemove}> Small </CwTag> <CwTag size="medium" removable onCwRemove={handleRemove}> Medium </CwTag> <CwTag size="large" removable onCwRemove={handleRemove}> Large </CwTag> </div> <style>{css}</style> </> ); };
Importing
If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.
To import this component from the CDN using a script tag:
<script type="module" src="https://cdn.jsdelivr.net/npm/@cordwainer/cw-elements@1.1.1/cdn/components/tag/tag.js"></script>
To import this component from the CDN using a JavaScript import:
import 'https://cdn.jsdelivr.net/npm/@cordwainer/cw-elements@1.1.1/cdn/components/tag/tag.js';
To import this component using a bundler:
import '@cordwainer/cw-elements/dist/components/tag/tag.js';
To import this component as a React component:
import CwTag from '@cordwainer/cw-elements/dist/react/tag';
Slots
| Name | Description |
|---|---|
| (default) | The tag’s content. |
Learn more about using slots.
Properties
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
variant
|
The tag’s theme variant. |
|
'primary' | 'success' | 'neutral' | 'warning' | 'danger' | 'text'
|
'neutral'
|
size
|
The tag’s size. |
|
'small' | 'medium' | 'large'
|
'medium'
|
pill
|
Draws a pill-style tag with rounded edges. |
|
boolean
|
false
|
removable
|
Makes the tag removable and shows a remove button. |
boolean
|
false
|
|
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Learn more about attributes and properties.
Events
| Name | React Event | Description | Event Detail |
|---|---|---|---|
cw-remove |
onCwRemove |
Emitted when the remove button is activated. | - |
Learn more about events.
Parts
| Name | Description |
|---|---|
base |
The component’s base wrapper. |
content |
The tag’s content. |
remove-button |
The tag’s remove button, an <cw-icon-button>. |
remove-button__base |
The remove button’s exported base part. |
Learn more about customizing CSS parts.
Dependencies
This component automatically imports the following dependencies.
<cw-icon><cw-icon-button>