Skip to content

Commit

Permalink
Merge pull request #16293 from craftcms/a11y/field-toggles
Browse files Browse the repository at this point in the history
Field toggle accessibility
  • Loading branch information
brandonkelly authored Dec 8, 2024
2 parents 0bc16d1 + a8e37e2 commit 76d17d5
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
- Improved the accessibility of Selectize inputs. ([#16110](https://github.com/craftcms/cms/pull/16110))
- Improved the accessibility of the image rotation control within the Image Editor. ([#16218](https://github.com/craftcms/cms/pull/16218))
- Improved the accessibility of action menus on the Plugins index page.
- Improved the accessibility of “More” and “Advanced” toggle triggers. ([#16293]](https://github.com/craftcms/cms/pull/16293))
- Improved the accessibility of the Craft Support widget. ([#16293]](https://github.com/craftcms/cms/pull/16293))

### Administration
- Added the “Show the ‘URL Suffix’ field” setting to Link fields. ([#15813](https://github.com/craftcms/cms/discussions/15813))
Expand Down
2 changes: 1 addition & 1 deletion src/fields/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public function getSettingsHtml(): ?string
'on' => $this->showTargetField,
]) .
Html::tag('hr') .
Html::a(Craft::t('app', 'Advanced'), options: [
Html::button(Craft::t('app', 'Advanced'), options: [
'class' => 'fieldtoggle',
'data' => ['target' => 'advanced'],
]) .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
{% block advancedSettings %}
<hr>

<a class="fieldtoggle" data-target="advanced">{{ "Advanced"|t('app') }}</a>
<button type="button" class="fieldtoggle" data-target="advanced">{{ "Advanced"|t('app') }}</button>
<div id="advanced" class="hidden">

{{ forms.lightswitchField({
Expand Down
2 changes: 1 addition & 1 deletion src/templates/_components/widgets/CraftSupport/body.twig
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
value: email
}) }}

<a class="fieldtoggle" data-target="{{ idPrefix }}-support-more">{{ "More"|t('app') }}</a>
<button type="button" class="fieldtoggle" data-target="{{ idPrefix }}-support-more">{{ "More"|t('app') }}</button>

<div id="{{ idPrefix }}-support-more" class="cs-support-more hidden">
<fieldset>
Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/cp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/cp.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/css/cp.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/css/cp.css.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/web/assets/cp/src/css/_main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1101,18 +1101,21 @@ button.toggle {
}

.toggle::before,
button.fieldtoggle::before,
a.fieldtoggle::before {
@include mixins.angle(right);
transition: transform linear 100ms;
}

.toggle.expanded::before,
button.fieldtoggle.expanded::before,
a.fieldtoggle.expanded::before,
.sidebar nav li.expanded > .toggle::before,
.structure li:not(.collapsed) > .row > .toggle::before {
transform: rotate(45deg) !important;
}

button.fieldtoggle,
a.fieldtoggle {
display: block;
position: relative;
Expand All @@ -1123,6 +1126,11 @@ a.fieldtoggle {
text-decoration: none;
}

button.fieldtoggle {
cursor: pointer;
}

button.fieldtoggle::before,
a.fieldtoggle::before {
display: block;
position: absolute;
Expand Down
56 changes: 46 additions & 10 deletions src/web/assets/cp/src/js/FieldToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,18 @@ Craft.FieldToggle = Garnish.Base.extend({
this.findTargets();

switch (this.type) {
case 'link':
this.addListener(this.$toggle, 'click', 'onToggleChange');
case 'button':
if (this._isButtonToggle()) {
if (!this._$target.attr('id')) {
this._$target.attr(
'id',
`toggle-target-${Math.floor(Math.random() * 1000000)}`
);
}
this.$toggle.attr('aria-controls', this._$target.attr('id'));
this._updateButtonExpanded();
}
this.addListener(this.$toggle, 'activate', 'onToggleChange');
break;
case 'fieldset':
this.addListener(
Expand All @@ -71,7 +81,7 @@ Craft.FieldToggle = Garnish.Base.extend({
},

getType: function () {
let nodeName = this.$toggle.prop('nodeName');
let nodeName = this._toggleNodeName();
if (
(nodeName === 'INPUT' && this.$toggle.attr('type') === 'checkbox') ||
this.$toggle.attr('role') === 'checkbox' ||
Expand All @@ -86,8 +96,9 @@ Craft.FieldToggle = Garnish.Base.extend({
return 'booleanMenu';
}
return 'select';
case 'BUTTON':
case 'A':
return 'link';
return 'button';
default:
return 'fieldset';
}
Expand Down Expand Up @@ -152,10 +163,8 @@ Craft.FieldToggle = Garnish.Base.extend({
} else {
this.findTargets();

if (this.type === 'link') {
this.onToggleChange._show =
this.$toggle.hasClass('collapsed') ||
!this.$toggle.hasClass('expanded');
if (this.type === 'button') {
this.onToggleChange._show = this._buttonIsCollapsed();
} else if (this.type === 'checkbox' && this.targetPrefix !== null) {
this.onToggleChange._show = this.$toggle.prop('checked');
} else {
Expand Down Expand Up @@ -183,9 +192,12 @@ Craft.FieldToggle = Garnish.Base.extend({
$target.removeClass('hidden');

if (this.type !== 'select' && this.type !== 'fieldset') {
if (this.type === 'link') {
if (this.type === 'button') {
this.$toggle.removeClass('collapsed');
this.$toggle.addClass('expanded');
if (this._isButtonToggle()) {
this._updateButtonExpanded();
}
}

for (let i = 0; i < $target.length; i++) {
Expand Down Expand Up @@ -229,9 +241,12 @@ Craft.FieldToggle = Garnish.Base.extend({
if (this.type === 'select' || this.type === 'fieldset') {
$target.addClass('hidden');
} else {
if (this.type === 'link') {
if (this.type === 'button') {
this.$toggle.removeClass('expanded');
this.$toggle.addClass('collapsed');
if (this._isButtonToggle()) {
this._updateButtonExpanded();
}
}

for (let i = 0; i < $target.length; i++) {
Expand All @@ -254,6 +269,27 @@ Craft.FieldToggle = Garnish.Base.extend({
}
},

_toggleNodeName: function () {
return this.$toggle.prop('nodeName');
},

_isButtonToggle: function () {
return this._toggleNodeName() === 'BUTTON';
},

_buttonIsCollapsed: function () {
return (
this.$toggle.hasClass('collapsed') || !this.$toggle.hasClass('expanded')
);
},

_updateButtonExpanded() {
this.$toggle.attr(
'aria-expanded',
this._buttonIsCollapsed() ? 'false' : 'true'
);
},

destroy: function () {
this.$toggle.removeData('fieldtoggle');
this.base();
Expand Down
Loading

0 comments on commit 76d17d5

Please sign in to comment.