package examples import (package examples import ( "github.com/axadrn/shadcn-templ/v2/components/icon" "github.com/axadrn/shadcn-templ/v2/components/togglegroup") templ ToggleGroupDemo() { @togglegroup.ToggleGroup(togglegroup.Props{ Variant: togglegroup.VariantOutline, ToggleMultiple: true, }) { @togglegroup.Item(togglegroup.ItemProps{ Value: "bold", Attributes: templ.Attributes{"aria-label": "Toggle bold"}, }) { @icon.Bold() } @togglegroup.Item(togglegroup.ItemProps{ Value: "italic", Attributes: templ.Attributes{"aria-label": "Toggle italic"}, }) { @icon.Italic() } @togglegroup.Item(togglegroup.ItemProps{ Value: "strikethrough", Attributes: templ.Attributes{"aria-label": "Toggle strikethrough"}, }) { @icon.Underline() } }}Installation
Usage
Composition
Use the following composition to build a ToggleGroup:
Outline
Use Variant: togglegroup.VariantOutline for an outline style.
package examples import (package examples import ( "github.com/axadrn/shadcn-templ/v2/components/togglegroup") templ ToggleGroupOutline() { @togglegroup.ToggleGroup(togglegroup.Props{ Variant: togglegroup.VariantOutline, }) { @togglegroup.Item(togglegroup.ItemProps{ Value: "all", Pressed: true, Attributes: templ.Attributes{"aria-label": "Toggle all"}, }) { All } @togglegroup.Item(togglegroup.ItemProps{ Value: "missed", Attributes: templ.Attributes{"aria-label": "Toggle missed"}, }) { Missed } }}Size
Use the Size prop to change the size of the toggle group.
package examples import (package examples import ( "github.com/axadrn/shadcn-templ/v2/components/togglegroup") templ ToggleGroupSize() { <div class="flex flex-col gap-4"> @togglegroup.ToggleGroup(togglegroup.Props{ Size: togglegroup.SizeSm, Variant: togglegroup.VariantOutline, }) { @togglegroup.Item(togglegroup.ItemProps{ Value: "top", Pressed: true, Attributes: templ.Attributes{"aria-label": "Toggle top"}, }) { Top } @togglegroup.Item(togglegroup.ItemProps{ Value: "bottom", Attributes: templ.Attributes{"aria-label": "Toggle bottom"}, }) { Bottom } @togglegroup.Item(togglegroup.ItemProps{ Value: "left", Attributes: templ.Attributes{"aria-label": "Toggle left"}, }) { Left } @togglegroup.Item(togglegroup.ItemProps{ Value: "right", Attributes: templ.Attributes{"aria-label": "Toggle right"}, }) { Right } } @togglegroup.ToggleGroup(togglegroup.Props{ Variant: togglegroup.VariantOutline, }) { @togglegroup.Item(togglegroup.ItemProps{ Value: "top", Pressed: true, Attributes: templ.Attributes{"aria-label": "Toggle top"}, }) { Top } @togglegroup.Item(togglegroup.ItemProps{ Value: "bottom", Attributes: templ.Attributes{"aria-label": "Toggle bottom"}, }) { Bottom } @togglegroup.Item(togglegroup.ItemProps{ Value: "left", Attributes: templ.Attributes{"aria-label": "Toggle left"}, }) { Left } @togglegroup.Item(togglegroup.ItemProps{ Value: "right", Attributes: templ.Attributes{"aria-label": "Toggle right"}, }) { Right } } </div>}Spacing
Use Spacing to add spacing between toggle group items.
package examples import (package examples import ( "github.com/axadrn/shadcn-templ/v2/components/togglegroup" "github.com/axadrn/shadcn-templ/v2/utils") templ ToggleGroupSpacing() { @togglegroup.ToggleGroup(togglegroup.Props{ Size: togglegroup.SizeSm, Variant: togglegroup.VariantOutline, Spacing: utils.Ptr(2), }) { @togglegroup.Item(togglegroup.ItemProps{ Value: "top", Pressed: true, Attributes: templ.Attributes{"aria-label": "Toggle top"}, }) { Top } @togglegroup.Item(togglegroup.ItemProps{ Value: "bottom", Attributes: templ.Attributes{"aria-label": "Toggle bottom"}, }) { Bottom } @togglegroup.Item(togglegroup.ItemProps{ Value: "left", Attributes: templ.Attributes{"aria-label": "Toggle left"}, }) { Left } @togglegroup.Item(togglegroup.ItemProps{ Value: "right", Attributes: templ.Attributes{"aria-label": "Toggle right"}, }) { Right } }}Vertical
Use Orientation: togglegroup.OrientationVertical for vertical toggle groups.
package examples import (package examples import ( "github.com/axadrn/shadcn-templ/v2/components/icon" "github.com/axadrn/shadcn-templ/v2/components/togglegroup" "github.com/axadrn/shadcn-templ/v2/utils") templ ToggleGroupVertical() { @togglegroup.ToggleGroup(togglegroup.Props{ ToggleMultiple: true, Orientation: togglegroup.OrientationVertical, Spacing: utils.Ptr(1), }) { @togglegroup.Item(togglegroup.ItemProps{ Value: "bold", Pressed: true, Attributes: templ.Attributes{"aria-label": "Toggle bold"}, }) { @icon.Bold() } @togglegroup.Item(togglegroup.ItemProps{ Value: "italic", Pressed: true, Attributes: templ.Attributes{"aria-label": "Toggle italic"}, }) { @icon.Italic() } @togglegroup.Item(togglegroup.ItemProps{ Value: "underline", Attributes: templ.Attributes{"aria-label": "Toggle underline"}, }) { @icon.Underline() } }}Disabled
package examples import (package examples import ( "github.com/axadrn/shadcn-templ/v2/components/icon" "github.com/axadrn/shadcn-templ/v2/components/togglegroup") templ ToggleGroupDisabled() { @togglegroup.ToggleGroup(togglegroup.Props{ToggleMultiple: true}) { @togglegroup.Item(togglegroup.ItemProps{ Value: "bold", Disabled: true, Attributes: templ.Attributes{"aria-label": "Toggle bold"}, }) { @icon.Bold() } @togglegroup.Item(togglegroup.ItemProps{ Value: "italic", Disabled: true, Attributes: templ.Attributes{"aria-label": "Toggle italic"}, }) { @icon.Italic() } @togglegroup.Item(togglegroup.ItemProps{ Value: "strikethrough", Disabled: true, Attributes: templ.Attributes{"aria-label": "Toggle strikethrough"}, }) { @icon.Underline() } }}Custom
A custom toggle group example.
Use font-normal to set the font weight.
package examples import (package examples import ( "github.com/axadrn/shadcn-templ/v2/components/field" "github.com/axadrn/shadcn-templ/v2/components/togglegroup" "github.com/axadrn/shadcn-templ/v2/utils") templ ToggleGroupCustom() { @field.Field(field.Props{Class: "max-w-xs"}) { @field.Label() { Font Weight } @togglegroup.ToggleGroup(togglegroup.Props{ ID: "font-weight-group", Variant: togglegroup.VariantOutline, Size: togglegroup.SizeLg, Spacing: utils.Ptr(2), }) { @togglegroup.Item(togglegroup.ItemProps{ Value: "light", Class: "flex size-16 flex-col items-center justify-center rounded-xl", Attributes: templ.Attributes{"aria-label": "Light"}, }) { <span class="text-2xl leading-none font-light">Aa</span> <span class="text-xs text-muted-foreground">Light</span> } @togglegroup.Item(togglegroup.ItemProps{ Value: "normal", Pressed: true, Class: "flex size-16 flex-col items-center justify-center rounded-xl", Attributes: templ.Attributes{"aria-label": "Normal"}, }) { <span class="text-2xl leading-none font-normal">Aa</span> <span class="text-xs text-muted-foreground">Normal</span> } @togglegroup.Item(togglegroup.ItemProps{ Value: "medium", Class: "flex size-16 flex-col items-center justify-center rounded-xl", Attributes: templ.Attributes{"aria-label": "Medium"}, }) { <span class="text-2xl leading-none font-medium">Aa</span> <span class="text-xs text-muted-foreground">Medium</span> } @togglegroup.Item(togglegroup.ItemProps{ Value: "bold", Class: "flex size-16 flex-col items-center justify-center rounded-xl", Attributes: templ.Attributes{"aria-label": "Bold"}, }) { <span class="text-2xl leading-none font-bold">Aa</span> <span class="text-xs text-muted-foreground">Bold</span> } } @field.Description() { Use <code id="font-weight-code" class="rounded-md bg-muted px-1 py-0.5 font-mono">font-normal</code> to set the font weight. } } <script nonce={ templ.GetNonce(ctx) }> (() => { const group = document.getElementById("font-weight-group"); const code = document.getElementById("font-weight-code"); group.addEventListener("toggle-change", () => { const value = group.getAttribute("data-tui-toggle-group-value"); if (value) code.textContent = "font-" + value; }); })(); </script>}API Reference
ToggleGroup
The togglegroup.ToggleGroup component wraps its items into a single or multiple selection group.