Files
hinode/layouts/shortcodes/button.html
2023-08-31 13:34:23 +02:00

150 lines
6.1 KiB
HTML

<!--
Displays a button. The shortcode supports the following arguments:
"href" Optional address for the button or hyperlink. Automatically assigned when using collapse.
"relref" Optional name of the page to link to. Replaces "href" with a relative link if set.
"id" Optional id of the button, to be used in the DOM.
"state" Optional state of the button, either "enabled" (default), "disabled", "active", or "inactive".
"size" Optional size of the button, either "sm", "md" (default), or "lg".
"color": Optional theme color of the element, either "primary" (default), "secondary", "success",
"danger", "warning", "info", "light", "dark", "white", or "black".
"outline" Optional flag indicating the button should be outlined, either "false" (default) or "true".
"badge" Optional positioned badge to display on top of the button.
"aria-label" Optional label for the badge.
"tooltip" Optional text to display in a tooltip. Cannot be used together with collapse. Ignored for active/
inactive buttons.
"collapse" Optional panel to collapse. Cannot be used together with tooltip. Ignored for active/inactive
buttons.
"placement" Optional position of the tooltip: "top" (default), "bottom", "left", or "right".
"class" Optional class attribute of the button element, e.g. “p-5”.
"icon" Font Awesome icon class attribute, required unless button title is set. An example value is
"fas sort".
"order" Optional order of the icon, either "first" or "last" (default).
"justify" Optional justification of the button title and icon, either "start", "end", "center" (default),
"between", "around", or "evenly".
"toast" Optional id of the toast to display when the button is clicked.
"cue" Optional flag to indicate if an external link should show a visual cue, defaults to setting
"main.externalLinks.cue" in the site's parameters.
"tab" Optional flag to indicate if an external link should open in a new tab, defaults to setting
"main.externalLinks.tab" in the site's parameters.
-->
{{ $error := false }}
{{ $type := "button" -}}
{{- $icon := .Get "icon" | default "" }}
{{ $title := trim .Inner " \r\n" | .Page.RenderString -}}
{{ if not (or $title $icon) -}}
{{ errorf "Missing icon or inner element text: %s" .Position -}}
{{ $error = true }}
{{ end -}}
{{ $state := "enabled" -}}
{{ with .Get "state" }}{{ $state = . }}{{ end -}}
{{ $supportedStates := slice "enabled" "disabled" "active" "inactive" -}}
{{ if not (in $supportedStates $state) -}}
{{ errorf "Invalid value for param 'state': %s" .Position -}}
{{ $error = true }}
{{ end -}}
{{ $size := "md" -}}
{{ with .Get "size" }}{{ $size = . }}{{ end -}}
{{ $supportedSizes := slice "sm" "md" "lg" -}}
{{ if not (in $supportedSizes $size) -}}
{{ errorf "Invalid value for param 'size': %s" .Position -}}
{{ end -}}
{{ $color := "primary" -}}
{{ with .Get "color" }}{{ $color = . }}{{ end -}}
{{ $supportedColors := slice "primary" "secondary" "success" "danger" "warning" "info" "light" "dark" "white" "black" -}}
{{ if not (in $supportedColors $color) -}}
{{ errorf "Invalid value for param 'color': %s" .Position -}}
{{ $error = true }}
{{ end -}}
{{ $outlineParam := "false" -}}
{{ $outline := false -}}
{{ with .Get "outline" }}{{ $outlineParam = . }}{{ end -}}
{{ $supportedFlags := slice "true" "false" -}}
{{ if in $supportedFlags $outlineParam -}}
{{ if eq $outlineParam "true" }}{{ $outline = true }}{{ end -}}
{{ else -}}
{{ errorf "Invalid value for param 'outline': %s" .Position -}}
{{ end -}}
{{ $badge := .Get "badge" | default "" -}}
{{ $label := .Get "aria-label" | default "" -}}
{{ $tooltip := .Get "tooltip" | default "" -}}
{{ $collapse := .Get "collapse" | default "" -}}
{{ $href := .Get "href" | default "" -}}
{{ $relref := .Get "relref" | default "" -}}
{{ $id := .Get "id" | default "" -}}
{{ if $relref }}
{{ $href = relref . $relref }}
{{ end }}
{{ $class := .Get "class" | default "" -}}
{{ $placement := "top" -}}
{{ with .Get "placement" }}{{ $placement = . }}{{ end -}}
{{ $supportedPlacements := slice "top" "bottom" "left" "right" -}}
{{ if not (in $supportedPlacements $placement) -}}
{{ errorf "Invalid value for param 'placement': %s" .Position -}}
{{ $error = true }}
{{ end -}}
{{- $toast := .Get "toast" | default "" -}}
{{- $order := "last" -}}
{{- with .Get "order" }}{{ $order = . }}{{ end -}}
{{- $supportedOrders := slice "first" "last" -}}
{{- if not (in $supportedOrders $order) -}}
{{ errorf "Invalid value for param 'order': %s" .Position -}}
{{ $error = true }}
{{- end -}}
{{- $justify := "center" -}}
{{- with .Get "justify" }}{{ $justify = . }}{{ end -}}
{{- $supportedJustify := slice "start" "end" "center" "between" "around" "evenly" -}}
{{- if not (in $supportedJustify $justify) -}}
{{ errorf "Invalid value for param 'justify': %s" .Position -}}
{{ $error = true }}
{{- end -}}
{{- $cue := .Get "cue" | default site.Params.main.externalLinks.cue -}}
{{- $tab := .Get "tab" | default site.Params.main.externalLinks.tab -}}
{{ if not $error }}
{{- $output := partial "assets/button.html" (dict
"type" $type
"title" $title
"size" $size
"color" $color
"outline" $outline
"badge" $badge
"label" $label
"tooltip" $tooltip
"collapse" $collapse
"href" $href
"id" $id
"state" $state
"class" $class
"placement" $placement
"icon" $icon
"order" $order
"justify" $justify
"cue" $cue
"tab" $tab
"toast" $toast)
-}}
{{- if .Parent -}}
{{ $current := .Parent.Scratch.Get "inner" }}
{{ if $current }}
{{ .Parent.Scratch.Set "inner" (print $current $output) }}
{{ else }}
{{ .Parent.Scratch.Set "inner" $output }}
{{ end }}
{{- else -}}
{{ $output | safeHTML }}
{{- end -}}
{{ end }}