Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A11Y: Improvements to <DToggleSwitch/> component #23514

Merged
merged 6 commits into from Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,42 @@
import Component from "@glimmer/component";
import I18n from "I18n";
import icon from "discourse-common/helpers/d-icon";

export default class DToggleSwitch extends Component {
<template>
<div class="d-toggle-switch">
<label class="d-toggle-switch--label">
{{! template-lint-disable no-redundant-role }}
<button
class="d-toggle-switch__checkbox"
type="button"
role="switch"
aria-checked={{this.checked}}
...attributes
></button>
{{! template-lint-enable no-redundant-role }}

<span class="d-toggle-switch__checkbox-slider">
{{#if @state}}
{{icon "check"}}
{{/if}}
</span>
</label>

<span class="d-toggle-switch__checkbox-label">
{{this.computedLabel}}
</span>
</div>
</template>

get computedLabel() {
if (this.args.label) {
return I18n.t(this.args.label);
}
return this.args.translatedLabel;
}

get checked() {
return this.args.state ? "true" : "false";
}
}

This file was deleted.

15 changes: 0 additions & 15 deletions app/assets/javascripts/discourse/app/components/d-toggle-switch.js

This file was deleted.

25 changes: 23 additions & 2 deletions app/assets/stylesheets/common/components/d-toggle-switch.scss
Expand Up @@ -5,6 +5,7 @@
&:focus {
.d-toggle-switch__checkbox-slider {
outline: 2px solid var(--tertiary);
outline-offset: 2px;
}
}

Expand All @@ -21,6 +22,7 @@

display: flex;
align-items: center;
gap: 0.5rem;

&__label {
position: relative;
Expand All @@ -30,7 +32,23 @@

&__checkbox {
position: absolute;
visibility: hidden;
border: 0;
padding: 0;
background: transparent;

&:focus {
+ .d-toggle-switch__checkbox-slider {
outline: 2px solid var(--tertiary);
outline-offset: 2px;
}

// Outline should show only when tabbing, not clicking
&:not(:focus-visible) {
+ .d-toggle-switch__checkbox-slider {
outline: none;
}
}
}
}

&__checkbox[aria-checked="true"] + .d-toggle-switch__checkbox-slider {
Expand All @@ -57,7 +75,6 @@
border-radius: var(--toggle-switch-height);
width: var(--toggle-switch-width);
height: var(--toggle-switch-height);
margin-right: 0.5em;
position: relative;
vertical-align: middle;
transition: background 0.25s;
Expand Down Expand Up @@ -87,5 +104,9 @@
top: calc(var(--toggle-switch-height) * 0.125);
left: calc(var(--toggle-switch-height) * 0.125);
transition: left 0.25s;

@media (prefers-reduced-motion: reduce) {
transition-duration: 0ms;
}
}
}