Skip to content

Commit

Permalink
Helper and utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mdo committed Nov 12, 2022
1 parent a6177db commit 643bc14
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 10 deletions.
1 change: 1 addition & 0 deletions scss/_helpers.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@import "helpers/clearfix";
@import "helpers/color-bg";
@import "helpers/colored-links";
@import "helpers/focus-ring";
@import "helpers/ratio";
@import "helpers/position";
@import "helpers/stacks";
Expand Down
17 changes: 10 additions & 7 deletions scss/_root.scss
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@

// Focus styles
// scss-docs-start root-focus-variables
--#{$variable-prefix}focus-ring-width: #{$focus-ring-width};
--#{$variable-prefix}focus-ring-opacity: #{$focus-ring-opacity};
--#{$variable-prefix}focus-ring-color: #{$focus-ring-color};
--#{$variable-prefix}focus-ring-blur: #{$focus-ring-blur};
// By default, there is no `--bs-focus-ring-offset-x` or `--bs-focus-ring-offset-y`, but we provide CSS variables with fallbacks to initial `0` values
--#{$variable-prefix}focus-ring-box-shadow: var(--#{$variable-prefix}focus-ring-offset-x, 0) var(--#{$variable-prefix}focus-ring-offset-y, 0) var(--#{$variable-prefix}focus-ring-blur) var(--#{$variable-prefix}focus-ring-width) var(--#{$variable-prefix}focus-ring-color);
--#{$prefix}focus-ring-width: #{$focus-ring-width};
--#{$prefix}focus-ring-opacity: #{$focus-ring-opacity};
--#{$prefix}focus-ring-color: #{$focus-ring-color};
--#{$prefix}focus-ring-blur: #{$focus-ring-blur};
// --#{$prefix}focus-ring-offset: 0 0 0 1px var(--#{$prefix}focus-ring-offset-color, var(--#{$prefix}body-bg));
--#{$prefix}focus-ring-offset-width: 1px;
--#{$prefix}focus-ring-offset-color: var(--#{$prefix}body-bg);

// By default, there is no `--bs-focus-ring-x` or `--bs-focus-ring-y`, but we provide CSS variables with fallbacks to initial `0` values
--#{$prefix}focus-ring-box-shadow: 0 0 0 var(--#{$prefix}focus-ring-offset-width) var(--#{$prefix}focus-ring-offset-color), var(--#{$prefix}focus-ring-x, 0) var(--#{$prefix}focus-ring-y, 0) var(--#{$prefix}focus-ring-blur) var(--#{$prefix}focus-ring-width) var(--#{$prefix}focus-ring-color);
// scss-docs-end root-focus-variables
// stylelint-enable custom-property-empty-line-before
}
3 changes: 2 additions & 1 deletion scss/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ $focus-ring-width: .25rem !default;
$focus-ring-opacity: .25 !default;
$focus-ring-color: rgba($primary, $focus-ring-opacity) !default;
$focus-ring-blur: 0 !default;
$focus-ring-box-shadow: 0 0 $focus-ring-blur $focus-ring-width $focus-ring-color !default;
$focus-ring-offset: 0 0 0 1px var(--#{$prefix}focus-ring-offset-color, var(--#{$prefix}body-bg)) !default;
$focus-ring-box-shadow: $focus-ring-offset, 0 0 $focus-ring-blur $focus-ring-width $focus-ring-color !default;
// scss-docs-end focus-ring-variables

// scss-docs-start caret-variables
Expand Down
16 changes: 16 additions & 0 deletions scss/helpers/_focus-ring.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// stylelint-disable indentation, function-disallowed-list, declaration-colon-newline-after, value-list-comma-newline-after, value-list-comma-space-after

.focus-ring:focus {
outline: 0;
// By default, there is no `--bs-focus-ring-x` or `--bs-focus-ring-y`, but we provide CSS variables with fallbacks to initial `0` values
box-shadow: 0 0 0 var(--#{$prefix}focus-ring-offset-width) var(--#{$prefix}focus-ring-offset-color, var(--#{$prefix}body-bg)),
var(--#{$prefix}focus-ring-x, 0)
var(--#{$prefix}focus-ring-y, 0)
var(--#{$prefix}focus-ring-blur)
calc(var(--#{$prefix}focus-ring-width) + var(--#{$prefix}focus-ring-offset-width))
var(--#{$prefix}focus-ring-color);
}

@each $state, $value in $theme-colors {
.focus-ring-#{$state}:focus { --#{$prefix}focus-ring-color: rgba(#{to-rgb($value)}, var(--bs-focus-ring-opacity)); }
}
5 changes: 5 additions & 0 deletions site/assets/scss/_component-examples.scss
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,8 @@
border: solid $border-color;
border-width: 1px 0;
}

.focused {
outline: 0;
box-shadow: var(--#{$variable-prefix}focus-ring-offset), var(--#{$variable-prefix}focus-ring-x, 0) var(--#{$variable-prefix}focus-ring-y, 0) var(--#{$variable-prefix}focus-ring-blur) var(--#{$variable-prefix}focus-ring-width) var(--#{$variable-prefix}focus-ring-color);
}
4 changes: 2 additions & 2 deletions site/content/docs/5.2/customize/css-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ a {

## Focus variables

<small class="d-inline-flex px-2 py-1 font-monospace text-muted border rounded-3">Added in v5.3.0</small>
{{< added-in "5.3.0" >}}

Bootstrap provides custom `:focus` styles using a combination of Sass and CSS variables that can be optionally added to specific components and elements. We do not yet globally override all `:focus ` styles.

In our Sass, we set default values that can be customized pre-compiling.
In our Sass, we set default values that can be customized before compiling.

{{< scss-docs name="focus-ring-variables" file="scss/_variables.scss" >}}

Expand Down
49 changes: 49 additions & 0 deletions site/content/docs/5.2/helpers/focus-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
layout: docs
title: Focus ring
description: Utility classes that allows you to add and modify custom focus ring styles to elements and components.
group: helpers
toc: true
---

The `.focus-ring` helper removes the default `outline` on `:focus`, replacing it with a `box-shadow` that can be more broadly customized. The new shadow is made up of a series of CSS variables, inherited from the `:root` level, that can be modified for any element or component.

## Example

Click into the example below and press <kbd>Tab</kbd> to see the focus ring in action.

{{< example >}}
<a href="#" class="d-inline-flex focus-ring py-1 px-2 text-decoration-none border rounded-2">
Custom focus ring
</a>
{{< /example >}}

## Customize

Modify the styling of a focus ring with our CSS variables, Sass variables, utilities, or custom styles.

### CSS variables

Modify the `--bs-focus-ring-*` CSS variables as needed to change the default appearance.

{{< example >}}
<a href="#" class="d-inline-flex focus-ring py-1 px-2 text-decoration-none border rounded-2" style="--bs-focus-ring-color: rgba(var(--bs-success-rgb), .25)">
Green focus ring
</a>
{{< /example >}}

### Sass

Customize the Sass variables to modify all usage of the focus ring styles across your Bootstrap-powered project.

{{< scss-docs name="focus-ring-variables" file="scss/_variables.scss" >}}

### Utilities

In addition to `.focus-ring`, we have several `.focus-ring-*` utilities to modify the helper class defaults. Modify the color with any of our theme colors.

{{< example >}}
<a href="#" class="d-inline-flex focus-ring focus-ring-danger py-1 px-2 text-decoration-none border rounded-2">
Danger focus ring
</a>
{{< /example >}}
1 change: 1 addition & 0 deletions site/data/sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
- title: Display
- title: Flex
- title: Float
- title: Focus ring
- title: Interactions
- title: Object fit
- title: Opacity
Expand Down

0 comments on commit 643bc14

Please sign in to comment.