Skip to content

Commit

Permalink
feat(close button): add a "Close" tooltip (#1823)
Browse files Browse the repository at this point in the history
* Add tooltip on close btn for button close + modals + alerts + toasts + offcanvas.

Co-authored-by: Julien Déramond <[email protected]>
  • Loading branch information
hannahiss and julien-deramond authored Jun 19, 2023
1 parent 360400d commit 9cda576
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 101 deletions.
19 changes: 12 additions & 7 deletions site/assets/js/snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,37 @@
// -------------------------------
// Used in 'Show live alert' example in docs or StackBlitz

// Boosted mod: adapted innerHTML to have the icon and so added a parameter within `alert()` function
// Boosted mod: adapted innerHTML to have the icon and so added a parameter within `appendAlert()` and a tooltip on `.btn-close`
// js-docs-start live-alert
const alertPlaceholder = document.getElementById('liveAlertPlaceholder')
const alert = (message, type, typeVisuallyHidden) => {
const appendAlert = (message, type, typeVisuallyHidden) => {
const wrapper = document.createElement('div')
wrapper.innerHTML = [
`<div class="alert alert-${type} alert-dismissible" role="alert">`,
' <span class="alert-icon">',
` <span class="visually-hidden">${typeVisuallyHidden}</span>`,
' </span>',
' <p>',
` ${message}`,
' </p>',
' <button type="button" class="btn-close" data-bs-dismiss="alert">',
` <p>${message}</p>`,
' <button type="button" class="btn-close" data-bs-dismiss="alert" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Close">',
' <span class="visually-hidden">Close</span>',
' </button>',
'</div>'
].join('')

alertPlaceholder.append(wrapper)

// Create `.btn-close` tooltip after `innerHTML` has been modified
const btnClose = wrapper.querySelector('.btn-close')
const tooltip = new boosted.Tooltip(btnClose)
btnClose.addEventListener('click', () => {
tooltip.hide()
})
}

const alertTrigger = document.getElementById('liveAlertBtn')
if (alertTrigger) {
alertTrigger.addEventListener('click', () => {
alert('Nice, you triggered this alert message!', 'success', 'Success')
appendAlert('Nice, you triggered this alert message!', 'success', 'Success')
})
}
// js-docs-end live-alert
Expand Down
11 changes: 7 additions & 4 deletions site/content/docs/5.3/components/alerts.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,32 @@ Using the alert JavaScript plugin, it's possible to dismiss any alert inline. He
- Be sure you've loaded the alert plugin, or the compiled Boosted JavaScript.
- Add a [close button]({{< docsref "/components/close-button" >}}) and the `.alert-dismissible` class, which adds extra padding to the right of the alert and positions the close button.
- On the close button, add the `data-bs-dismiss="alert"` attribute, which triggers the JavaScript functionality. Be sure to use the `<button>` element with it for proper behavior across all devices.
- On the close button, add the `data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Close"` attributes, which add a tooltip for better accessibility. You also need to [enable tooltips]({{< docsref "/components/tooltips#enable-tooltips" >}}) (if not already done).
- To animate alerts when dismissing them, be sure to add the `.fade` and `.show` classes.

You can see this in action with a live demo:

{{< example >}}
{{< example stackblitz_add_js="true" >}}
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<span class="alert-icon"><span class="visually-hidden">Warning</span></span>
<p>Warning notification text goes here.</p>
<button type="button" class="btn-close" data-bs-dismiss="alert"><span class="visually-hidden">Close</span></button>
<button type="button" class="btn-close" data-bs-dismiss="alert" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Close"><span class="visually-hidden">Close</span></button>
</div>
{{< /example >}}

{{< callout warning >}}
When an alert is dismissed, the element is completely removed from the page structure. If a keyboard user dismisses the alert using the close button, their focus will suddenly be lost and, depending on the browser, reset to the start of the page/document. For this reason, we recommend including additional JavaScript that listens for the `closed.bs.alert` event and programmatically sets `focus()` to the most appropriate location in the page. If you're planning to move focus to a non-interactive element that normally does not receive focus, make sure to add `tabindex="-1"` to the element.
{{< /callout >}}

{{% enable-btn-close-tooltip 4 alerts %}}

## Dark variant

{{< added-in "5.2.0" >}}

Add `.bg-dark` to the `.alert` for a dark variant. Close button can be inverted as well by using the [dark variant of close button]({{< docsref "/components/close-button#dark-variant" >}})

{{< example class="bg-dark" >}}
{{< example class="bg-dark" stackblitz_add_js="true" >}}
<div class="alert alert-success bg-dark" role="alert">
<span class="alert-icon"><span class="visually-hidden">Success</span></span>
<p>Success notification text goes here.</p>
Expand All @@ -147,7 +150,7 @@ Add `.bg-dark` to the `.alert` for a dark variant. Close button can be inverted
<div class="alert alert-warning alert-dismissible fade show bg-dark" role="alert">
<span class="alert-icon"><span class="visually-hidden">Warning</span></span>
<p>Warning notification text goes here.</p>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="alert"><span class="visually-hidden">Close</span></button>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="alert" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Close"><span class="visually-hidden">Close</span></button>
</div>
{{< /example >}}

Expand Down
22 changes: 12 additions & 10 deletions site/content/docs/5.3/components/close-button.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,45 @@ toc: true

Provide an option to dismiss or close a component with `.btn-close`. Default styling is limited, but highly customizable. Modify the Sass variables to replace the default `mask-image`. **Be sure to include text for screen readers**, as we've done with `<span class="visually-hidden">`.

{{< example >}}
<button type="button" class="btn-close"><span class="visually-hidden">Close</span></button>
{{< example stackblitz_add_js="true" >}}
<button type="button" class="btn-close" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Close"><span class="visually-hidden">Close</span></button>
{{< /example >}}

## Disabled state

Disabled close buttons change their `color`. We've also applied `pointer-events: none` and `user-select: none` to preventing hover and active states from triggering.

{{< example >}}
<button type="button" class="btn-close" disabled><span class="visually-hidden">Close</span></button>
{{< example stackblitz_add_js="true" >}}
<button type="button" class="btn-close" disabled data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Close"><span class="visually-hidden">Close</span></button>
{{< /example >}}

## Dark variant

Add `.btn-close-white` to the `.btn-close` for a dark variant.

{{< example class="bg-dark" >}}
<button type="button" class="btn-close btn-close-white"><span class="visually-hidden">Close</span></button>
<button type="button" class="btn-close btn-close-white" disabled><span class="visually-hidden">Close</span></button>
{{< example class="bg-dark" stackblitz_add_js="true" >}}
<button type="button" class="btn-close btn-close-white" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Close"><span class="visually-hidden">Close</span></button>
<button type="button" class="btn-close btn-close-white" disabled data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Close"><span class="visually-hidden">Close</span></button>
{{< /example >}}

## Without specific class

Close buttons can also be created without `.btn-close` to reduce the size of your CSS bundle.

{{< example >}}
<button type="button" class="btn btn-icon btn-no-outline">
{{< example stackblitz_add_js="true" >}}
<button type="button" class="btn btn-icon btn-no-outline" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Close">
<svg width="1.25rem" height="1.25rem" fill="currentColor" aria-hidden="true" focusable="false"><use xlink:href="/docs/{{< param docs_version >}}/assets/img/boosted-sprite.svg#delete"></use></svg>
<span class="visually-hidden">Close</span>
</button>

<button type="button" class="btn btn-icon btn-no-outline" disabled>
<button type="button" class="btn btn-icon btn-no-outline" disabled data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Close">
<svg width="1.25rem" height="1.25rem" fill="currentColor" aria-hidden="true" focusable="false"><use xlink:href="/docs/{{< param docs_version >}}/assets/img/boosted-sprite.svg#delete"></use></svg>
<span class="visually-hidden">Close</span>
</button>
{{< /example >}}

{{% enable-btn-close-tooltip 2 %}}

## CSS

### Variables
Expand Down
Loading

0 comments on commit 9cda576

Please sign in to comment.