bootstrap5/scss/mixins/_buttons.scss
2026-09-19 16:49:53 +08:00

126 lines
6.4 KiB
SCSS

// Button variants
//
// Easily pump out default styles, as well as :hover, :focus, :active,
// and disabled options for all buttons
// scss-docs-start btn-variant-mixin
@mixin button-variant(
$background,
$border,
$color: color-contrast($background),
$hover-background: if($color == $color-contrast-light, shade-color($background, $btn-hover-bg-shade-amount), tint-color($background, $btn-hover-bg-tint-amount)),
$hover-border: if($color == $color-contrast-light, shade-color($border, $btn-hover-border-shade-amount), tint-color($border, $btn-hover-border-tint-amount)),
$hover-color: color-contrast($hover-background),
$active-background: if($color == $color-contrast-light, shade-color($background, $btn-active-bg-shade-amount), tint-color($background, $btn-active-bg-tint-amount)),
$active-border: if($color == $color-contrast-light, shade-color($border, $btn-active-border-shade-amount), tint-color($border, $btn-active-border-tint-amount)),
$active-color: color-contrast($active-background),
$disabled-background: $background,
$disabled-border: $border,
$disabled-color: color-contrast($disabled-background)
) {
--#{$prefix}btn-color: #{$color};
--#{$prefix}btn-bg: #{$background};
--#{$prefix}btn-border-color: #{$border};
--#{$prefix}btn-hover-color: #{$hover-color};
--#{$prefix}btn-hover-bg: #{$hover-background};
--#{$prefix}btn-hover-border-color: #{$hover-border};
--#{$prefix}btn-focus-shadow-rgb: #{to-rgb(mix($color, $border, 15%))};
--#{$prefix}btn-active-color: #{$active-color};
--#{$prefix}btn-active-bg: #{$active-background};
--#{$prefix}btn-active-border-color: #{$active-border};
--#{$prefix}btn-active-shadow: #{$btn-active-box-shadow};
--#{$prefix}btn-disabled-color: #{$disabled-color};
--#{$prefix}btn-disabled-bg: #{$disabled-background};
--#{$prefix}btn-disabled-border-color: #{$disabled-border};
}
// scss-docs-end btn-variant-mixin
// Tinted "outline" button variant.
//
// This fork replaces Bootstrap's transparent outline buttons with a tinted
// style. The tints are the opaque color-mode custom properties from
// `_root.scss` (`-bg-subtle` / `-border-subtle` / `-text-emphasis`) rather
// than `rgba()` of the theme color, for two reasons: translucent fills
// composite over the parent element, so the button changes appearance on any
// non-white background, and the raw theme color as text fails WCAG on light
// fills (`$warning` is ~1.6:1 against white). The subtle/emphasis pair is
// opaque and pre-darkened, and it swaps under `[data-bs-theme="dark"]`.
//
// Intensity ladder: rest is the subtle fill, hover fills solid, active deepens
// the solid fill by the same shade/tint amounts the solid `.btn-*` variants
// use. Hover deliberately jumps to solid rather than to `-border-subtle`: that
// variable is a *border* color, not a background, and pairing it with
// `-text-emphasis` lands around 4.1:1 in dark mode where both are mid-tone.
//
// `$name` is the `$theme-colors` key (e.g. "primary") used to build the custom
// property names; `$color` is still needed for the focus ring and for
// `color-contrast()` on the solid active state.
// scss-docs-start btn-outline-variant-mixin
@mixin button-outline-variant(
$name,
$color,
$hover-background: $color,
$color-hover: color-contrast($hover-background),
$active-background: if(color-contrast($color) == $color-contrast-light, shade-color($color, $btn-active-bg-shade-amount), tint-color($color, $btn-active-bg-tint-amount)),
$active-border: $active-background,
$active-color: color-contrast($active-background)
) {
--#{$prefix}btn-color: var(--#{$prefix}#{$name}-text-emphasis);
--#{$prefix}btn-bg: var(--#{$prefix}#{$name}-bg-subtle);
--#{$prefix}btn-border-color: var(--#{$prefix}#{$name}-border-subtle);
--#{$prefix}btn-hover-color: #{$color-hover};
--#{$prefix}btn-hover-bg: #{$hover-background};
--#{$prefix}btn-hover-border-color: #{$hover-background};
--#{$prefix}btn-focus-shadow-rgb: #{to-rgb($color)};
--#{$prefix}btn-active-color: #{$active-color};
--#{$prefix}btn-active-bg: #{$active-background};
--#{$prefix}btn-active-border-color: #{$active-border};
--#{$prefix}btn-active-shadow: #{$btn-active-box-shadow};
--#{$prefix}btn-disabled-color: var(--#{$prefix}#{$name}-text-emphasis);
--#{$prefix}btn-disabled-bg: var(--#{$prefix}#{$name}-bg-subtle);
--#{$prefix}btn-disabled-border-color: var(--#{$prefix}#{$name}-border-subtle);
--#{$prefix}gradient: none;
}
// scss-docs-end btn-outline-variant-mixin
// Borderless, tinted button variant (`.btn-tint-*`).
// Same fill ladder as the outline variant with no border at all. Every border
// variable is declared explicitly as `transparent`: `.btn` only defaults
// `--#{$prefix}btn-border-color` and `--#{$prefix}btn-hover-border-color`, so
// leaving the active and disabled ones undeclared makes `border-color:
// var(…)` fall back to `currentColor` and draw a ring on `:active`.
// scss-docs-start btn-tint-variant-mixin
@mixin button-tint-variant(
$name,
$color,
$hover-background: $color,
$color-hover: color-contrast($hover-background),
$active-background: if(color-contrast($color) == $color-contrast-light, shade-color($color, $btn-active-bg-shade-amount), tint-color($color, $btn-active-bg-tint-amount)),
$active-color: color-contrast($active-background)
) {
--#{$prefix}btn-color: var(--#{$prefix}#{$name}-text-emphasis);
--#{$prefix}btn-bg: var(--#{$prefix}#{$name}-bg-subtle);
--#{$prefix}btn-border-color: transparent;
--#{$prefix}btn-hover-color: #{$color-hover};
--#{$prefix}btn-hover-bg: #{$hover-background};
--#{$prefix}btn-hover-border-color: transparent;
--#{$prefix}btn-focus-shadow-rgb: #{to-rgb($color)};
--#{$prefix}btn-active-color: #{$active-color};
--#{$prefix}btn-active-bg: #{$active-background};
--#{$prefix}btn-active-border-color: transparent;
--#{$prefix}btn-active-shadow: #{$btn-active-box-shadow};
--#{$prefix}btn-disabled-color: var(--#{$prefix}#{$name}-text-emphasis);
--#{$prefix}btn-disabled-bg: var(--#{$prefix}#{$name}-bg-subtle);
--#{$prefix}btn-disabled-border-color: transparent;
--#{$prefix}gradient: none;
}
// scss-docs-end btn-tint-variant-mixin
// scss-docs-start btn-size-mixin
@mixin button-size($padding-y, $padding-x, $font-size, $border-radius) {
--#{$prefix}btn-padding-y: #{$padding-y};
--#{$prefix}btn-padding-x: #{$padding-x};
@include rfs($font-size, --#{$prefix}btn-font-size);
--#{$prefix}btn-border-radius: #{$border-radius};
}
// scss-docs-end btn-size-mixin