Zum Inhaltsverzeichnis springen Button
<pc-button> 0.1.0 experimental Buttons repräsentieren Aktionen, die dem/der Nutzer:in zur Verfügung stehen.
<pc-button>Button</pc-button>
Demos#
Erscheinungsbild#
Verwende das appearance‐Attribut, um das Erscheinungsbild des Buttons festzulegen.
<pc-button appearance="neutral">Neutral</pc-button>
<pc-button appearance="primary">Primär</pc-button>
<pc-button appearance="success">Erfolg</pc-button>
<pc-button appearance="warning">Warnung</pc-button>
<pc-button appearance="danger">Gefahr</pc-button>
Varianten#
Verwende das variant‐Attribut, um die Variante des Buttons festzulegen.
<div style="margin-block-end: var(--pc-spacing-m)">
<pc-button appearance="primary" variant="accent">Akzent</pc-button>
<pc-button appearance="primary" variant="filled outlined">G + U</pc-button>
<pc-button appearance="primary" variant="filled">Gefüllt</pc-button>
<pc-button appearance="primary" variant="outlined">Umrandet</pc-button>
<pc-button appearance="primary" variant="plain">Schlicht</pc-button>
</div>
<div style="margin-block-end: var(--pc-spacing-m)">
<pc-button appearance="success" variant="accent">Akzent</pc-button>
<pc-button appearance="success" variant="filled outlined">G + U</pc-button>
<pc-button appearance="success" variant="filled">Gefüllt</pc-button>
<pc-button appearance="success" variant="outlined">Umrandet</pc-button>
<pc-button appearance="success" variant="plain">Schlicht</pc-button>
</div>
<div style="margin-block-end: var(--pc-spacing-m)">
<pc-button appearance="neutral" variant="accent">Akzent</pc-button>
<pc-button appearance="neutral" variant="filled outlined">G + U</pc-button>
<pc-button appearance="neutral" variant="filled">Gefüllt</pc-button>
<pc-button appearance="neutral" variant="outlined">Umrandet</pc-button>
<pc-button appearance="neutral" variant="plain">Schlicht</pc-button>
</div>
<div style="margin-block-end: var(--pc-spacing-m)">
<pc-button appearance="warning" variant="accent">Akzent</pc-button>
<pc-button appearance="warning" variant="filled outlined">G + U</pc-button>
<pc-button appearance="warning" variant="filled">Gefüllt</pc-button>
<pc-button appearance="warning" variant="outlined">Umrandet</pc-button>
<pc-button appearance="warning" variant="plain">Schlicht</pc-button>
</div>
<div>
<pc-button appearance="danger" variant="accent">Akzent</pc-button>
<pc-button appearance="danger" variant="filled outlined">G + U</pc-button>
<pc-button appearance="danger" variant="filled">Gefüllt</pc-button>
<pc-button appearance="danger" variant="outlined">Umrandet</pc-button>
<pc-button appearance="danger" variant="plain">Schlicht</pc-button>
</div>
Größen#
Verwende das size‐Attribut, um die Größe des Buttons zu ändern.
<pc-button size="small">Klein</pc-button>
<pc-button size="medium">Mittel</pc-button>
<pc-button size="large">Groß</pc-button>
Verwende das pill‐Attribut, um Buttons ein abgerundetes, pillenförmiges Aussehen zu verleihen.
<pc-button size="small" pill>Klein</pc-button>
<pc-button size="medium" pill>Mittel</pc-button>
<pc-button size="large" pill>Groß</pc-button>
Links#
Oft ist es hilfreich, einen Button zu haben, der wie ein Link funktioniert. Das ist möglich, indem du das href‐Attribut setzt, wodurch die Komponente unter der Haube als <a> gerendert wird. Dadurch erhältst du das gesamte Standard‐Link‐Verhalten des Browsers (z. B. Ctrl oder Shift + ) und kannst zudem die Attribute target und download nutzen.
<pc-button href="https://example.com">Link</pc-button>
<pc-button href="https://example.com" target="_blank">Neues Fenster</pc-button>
<pc-button href="https://placer-toolkit.vercel.app/brand/logo.svg" download>Download</pc-button>
<pc-button href="https://example.com" disabled>Deaktiviert</pc-button>
Wenn ein target gesetzt ist, erhält der Link aus Sicherheitsgründen automatisch rel="noreferrer noopener".
Wenn nur ein Icon in den Standard‐Slot eingefügt wird, wird der Button zu einem Icon‐Button. In diesem Fall ist es wichtig, dem Icon ein Label für Nutzer:innen mit assistiven Technologien zu geben. Icon‐Buttons können jedes Erscheinungsbild und jede Variante nutzen.
<div style="margin-block-end: var(--pc-spacing-m)">
<pc-button appearance="primary" variant="accent">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Akzent"
></pc-icon>
</pc-button>
<pc-button appearance="primary" variant="filled outlined">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Gefüllt + Umrandet"
></pc-icon>
</pc-button>
<pc-button appearance="primary" variant="filled">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Gefüllt"
></pc-icon>
</pc-button>
<pc-button appearance="primary" variant="outlined">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Umrandet"
></pc-icon>
</pc-button>
<pc-button appearance="primary" variant="plain">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Schlicht"
></pc-icon>
</pc-button>
</div>
<div style="margin-block-end: var(--pc-spacing-m)">
<pc-button appearance="success" variant="accent">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Akzent"
></pc-icon>
</pc-button>
<pc-button appearance="success" variant="filled outlined">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Gefüllt + Umrandet"
></pc-icon>
</pc-button>
<pc-button appearance="success" variant="filled">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Gefüllt"
></pc-icon>
</pc-button>
<pc-button appearance="success" variant="outlined">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Umrandet"
></pc-icon>
</pc-button>
<pc-button appearance="success" variant="plain">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Schlicht"
></pc-icon>
</pc-button>
</div>
<div style="margin-block-end: var(--pc-spacing-m)">
<pc-button appearance="neutral" variant="accent">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Akzent"
></pc-icon>
</pc-button>
<pc-button appearance="neutral" variant="filled outlined">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Gefüllt + Umrandet"
></pc-icon>
</pc-button>
<pc-button appearance="neutral" variant="filled">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Gefüllt"
></pc-icon>
</pc-button>
<pc-button appearance="neutral" variant="outlined">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Umrandet"
></pc-icon>
</pc-button>
<pc-button appearance="neutral" variant="plain">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Schlicht"
></pc-icon>
</pc-button>
</div>
<div style="margin-block-end: var(--pc-spacing-m)">
<pc-button appearance="warning" variant="accent">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Akzent"
></pc-icon>
</pc-button>
<pc-button appearance="warning" variant="filled outlined">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Gefüllt + Umrandet"
></pc-icon>
</pc-button>
<pc-button appearance="warning" variant="filled">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Gefüllt"
></pc-icon>
</pc-button>
<pc-button appearance="warning" variant="outlined">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Umrandet"
></pc-icon>
</pc-button>
<pc-button appearance="warning" variant="plain">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Schlicht"
></pc-icon>
</pc-button>
</div>
<div>
<pc-button appearance="danger" variant="accent">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Akzent"
></pc-icon>
</pc-button>
<pc-button appearance="danger" variant="filled outlined">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Gefüllt + Umrandet"
></pc-icon>
</pc-button>
<pc-button appearance="danger" variant="filled">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Gefüllt"
></pc-icon>
</pc-button>
<pc-button appearance="danger" variant="outlined">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Umrandet"
></pc-icon>
</pc-button>
<pc-button appearance="danger" variant="plain">
<pc-icon
library="default"
icon-style="solid"
name="house"
label="Schlicht"
></pc-icon>
</pc-button>
</div>
Benutzerdefinierte Breite#
Wie erwartet können Buttons eine benutzerdefinierte Breite erhalten, indem Inline‐Styles an die Komponente übergeben werden (oder eine CSS‐Klasse verwendet wird). Das ist nützlich, um Buttons auf kleineren Bildschirmen über die volle Breite ihres Containers zu strecken.
<pc-button size="small" style="inline-size: 100%; margin-block-end: var(--pc-spacing-m)">Klein</pc-button>
<pc-button size="medium" style="inline-size: 100%; margin-block-end: var(--pc-spacing-m)">Mittel</pc-button>
<pc-button size="large" style="inline-size: 100%">Groß</pc-button>
Präfixe und Suffixe#
Verwende die Slots prefix und suffix, um Icons hinzuzufügen.
<div style="margin-block-end: var(--pc-spacing-m)">
<pc-button size="small">
<pc-icon
library="default"
icon-style="solid"
name="gear"
slot="prefix"
></pc-icon>
Einstellungen
</pc-button>
<pc-button size="small">
<pc-icon
library="default"
icon-style="solid"
name="arrow-rotate-right"
slot="suffix"
></pc-icon>
Aktualisieren
</pc-button>
<pc-button size="small">
<pc-icon
library="default"
icon-style="solid"
name="link"
slot="prefix"
></pc-icon>
Öffnen
<pc-icon
library="default"
icon-style="solid"
name="arrow-up-right-from-square"
slot="suffix"
></pc-icon>
</pc-button>
</div>
<div style="margin-block-end: var(--pc-spacing-m)">
<pc-button size="medium">
<pc-icon
library="default"
icon-style="solid"
name="gear"
slot="prefix"
></pc-icon>
Einstellungen
</pc-button>
<pc-button size="medium">
<pc-icon
library="default"
icon-style="solid"
name="arrow-rotate-right"
slot="suffix"
></pc-icon>
Aktualisieren
</pc-button>
<pc-button size="medium">
<pc-icon
library="default"
icon-style="solid"
name="link"
slot="prefix"
></pc-icon>
Öffnen
<pc-icon
library="default"
icon-style="solid"
name="arrow-up-right-from-square"
slot="suffix"
></pc-icon>
</pc-button>
</div>
<div>
<pc-button size="large">
<pc-icon
library="default"
icon-style="solid"
name="gear"
slot="prefix"
></pc-icon>
Einstellungen
</pc-button>
<pc-button size="large">
<pc-icon
library="default"
icon-style="solid"
name="arrow-rotate-right"
slot="suffix"
></pc-icon>
Aktualisieren
</pc-button>
<pc-button size="large">
<pc-icon
library="default"
icon-style="solid"
name="link"
slot="prefix"
></pc-icon>
Öffnen
<pc-icon
library="default"
icon-style="solid"
name="arrow-up-right-from-square"
slot="suffix"
></pc-icon>
</pc-button>
</div>
Deaktiviert#
Verwende das disabled‐Attribut, um einen Button zu deaktivieren.
<pc-button appearance="neutral" disabled>Neutral</pc-button>
<pc-button appearance="primary" disabled>Primär</pc-button>
<pc-button appearance="success" disabled>Erfolg</pc-button>
<pc-button appearance="warning" disabled>Warnung</pc-button>
<pc-button appearance="danger" disabled>Gefahr</pc-button>
Eigene Styles#
Dieses Beispiel zeigt, wie du Buttons mit einer benutzerdefinierten Klasse stylen kannst. Dies ist der empfohlene Ansatz, wenn du zusätzliche Erscheinungsbilder hinzufügen möchtest. Um ein bestehendes Erscheinungsbild anzupassen, ändere den Selektor so, dass er das appearance‐Attribut des Buttons anspricht anstatt einer Klasse (z. B. pc-button[appearance="primary"]).
<pc-button class="awesome-button">Awesome‐Button</pc-button>
<style>
pc-button.awesome-button::part(base) {
background-color: var(--pc-color-warning-fill-normal);
color: var(--pc-color-warning-on-normal);
border: var(--pc-border-width-m) var(--pc-border-style)
var(--pc-color-warning-border-normal);
border-radius: 0.5rem;
box-shadow: 0 0.25rem 0 var(--pc-color-warning-border-normal);
transition: none;
}
@media (hover: hover) {
pc-button.awesome-button::part(base):hover {
background-color: color-mix(
in oklab,
var(--pc-color-warning-fill-normal),
var(--pc-color-mix-hover)
);
color: color-mix(
in oklab,
var(--pc-color-warning-on-normal),
var(--pc-color-mix-hover)
);
border-color: color-mix(
in oklab,
var(--pc-color-warning-border-normal),
var(--pc-color-mix-hover)
);
box-shadow: 0 0.25rem 0 color-mix(
in oklab,
var(--pc-color-warning-border-normal),
var(--pc-color-mix-hover)
);
}
}
pc-button.awesome-button::part(base):active {
background-color: color-mix(
in oklab,
var(--pc-color-warning-border-normal),
var(--pc-color-mix-hover)
);
color: color-mix(
in oklab,
var(--pc-color-warning-on-normal),
var(--pc-color-mix-hover)
);
border-color: color-mix(
in oklab,
var(--pc-color-warning-border-normal),
var(--pc-color-mix-hover)
);
box-shadow: none;
translate: 0 0.25rem;
}
</style>
Eigenschaften#
| Name | Beschreibung | Reflektiert | Standard |
|---|
appearance | The button’s appearance.
Typ: "primary"
| "success"
| "neutral"
| "warning"
| "danger"
| "text" |
| "neutral" |
variant | The button’s variant.
Typ: "accent"
| "filled"
| "outlined"
| "plain" |
| "accent" |
size | The button’s size.
Typ: "small" | "medium" | "large" |
| "medium" |
disabled | Disables the button.
Typ: boolean |
| false |
pill | Draws a pill‐style button.
Typ: boolean |
| false |
type | The type of button. Note that the default value is button instead of submit, which is opposite of how native <button> elements behave. When the type is submit, the button will submit the surrounding form.
Typ: "button" | "submit" | "reset" |
| "button" |
name | The name of the button, submitted as a name/value pair with form data, but only when this button is the submitter.
Typ: string |
| "" |
value | The value of the button, submitted as a pair with the button’s name as part of the form data, but only when this button is the submitter. This attribute is ignored when the href attribute is present.
Typ: string |
| "" |
href | When set, the underlying button will be rendered as an <a> with this href instead of a <button>.
Typ: string |
| "" |
target | Tells the browser where to open the link. It should only be used when the href attribute is present.
Typ: "_blank" | "_parent" | "_self" | "_top" | undefined |
| ‐ |
rel | When using the href attribute, this attribute will map to the underlying link’s rel attribute. Unlike regular links, the default set for this attribute is noreferrer noopener to prevent security exploits. However, if you’re using target to point to a specific tab or window, this will prevent that from working properly. You can remove or change the default value by setting the attribute to an empty string or a value of your choice, respectively.
Typ: string |
| "noreferrer noopener" |
download | Tells the browser to download the linked file as this file name. Only used when the href attribute is present.
Typ: string | undefined |
| ‐ |
form | This is the “form owner” to associate the button with. If omitted, the closest containing form will be used instead. The value of this attribute must be an id of a form in the same document or shadow root as the button.
Typ: string | undefined |
| ‐ |
formAction
formaction | Used to override the form owner’s action attribute.
Typ: string | undefined |
| ‐ |
formEnctype
formenctype | Used to override the form owner’s enctype attribute.
Typ: "application/x-www-form-url-encoded"
| "multipart/form-data"
| "text/plain" | undefined |
| ‐ |
formMethod
formmethod | Used to override the form owner’s method attribute.
Typ: "GET" | "POST" | undefined |
| ‐ |
formNoValidate
formnovalidate | Used to override the form owner’s novalidate attribute.
Typ: boolean | undefined |
| ‐ |
formTarget
formtarget | Used to override the form owner’s target attribute.
Typ: "_self"
| "_blank"
| "_parent"
| "_top"
| string | undefined |
| ‐ |
validity | Gets the validity state object. |
| ‐ |
validationMessage | Gets the validation message. |
| ‐ |
updateComplete |
Ein schreibgeschütztes Promise, das erfüllt ist,
sobald die Komponente
fertig aktualisiert wurde.
| | ‐ |
Erfahre mehr über Attribute und Eigenschaften.
Slots#
| Name | Beschreibung |
|---|
| (default) | The button’s label. |
prefix | A presentational prefix icon or similar element. |
suffix | A presentational suffix icon or similar element. |
Erfahre mehr über die Benutzung von Slots.
Methoden#
| Name | Beschreibung | Argumente |
|---|
click() | Simulates a click on the button. | ‐ |
focus() | Focuses the button. | options: FocusOptions |
blur() | Unfocuses the button (i.e., blurs it). | ‐ |
checkValidity() | Checks for validity but doesn’t show a validation message. Returns true when valid and false when invalid. | ‐ |
getForm() | Gets the associated form if one exists. | ‐ |
reportValidity() | Checks for validity and shows the browser’s validation message if the control is invalid. | ‐ |
setCustomValidity() | Sets a custom validation message. Pass an empty string to restore validity. | message: string |
Erfahre mehr über Methoden.
Events#
| Name | Beschreibung | Event‐Detail |
|---|
pc-focus | Emitted when the button gains focus. | ‐ |
pc-blur | Emitted when the button loses focus (i.e., is blurred). | ‐ |
pc-invalid | Emitted when the form control has been checked for validity and its constraints aren’t satisfied. | ‐ |
Erfahre mehr über Events.
Parts#
| Name | Beschreibung |
|---|
base | The component’s base wrapper. |
prefix | The container that wraps the prefix. |
label | The button’s label. |
suffix | The container that wraps the suffix. |
Erfahre mehr über das Anpassen von CSS‐Parts.
Importieren#
Wenn du den Autoloader oder den Standard‐Loader nutzt,
kannst du diesen Abschnitt überspringen. Falls du
„Cherry Picking“ betreibst, kannst du die folgenden
Snippets verwenden, um diese Komponente zu importieren.
CDN (Skript‐Tag)CDN (Import)npm (Import)
Um diese Komponente manuell vom CDN zu importieren,
kopiere dieses Code‐Snippet und füge es in dein HTML ein.
<script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/button/button.js"></script>
Um diese Komponente manuell vom CDN zu importieren, kopiere
dieses Code‐Snippet und füge es in deine JavaScript‐Datei ein.
import "https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/button/button.js";
Um diese Komponente manuell via npm zu importieren, kopiere
dieses Code‐Snippet und füge es in deine JavaScript‐Datei ein.
import "placer-toolkit/dist/components/button/button.js";