Skip to content

Instantly share code, notes, and snippets.

@yjbanov
Created May 7, 2024 18:19
Show Gist options
  • Save yjbanov/dc817bb344d1c84d60801a6b319e977a to your computer and use it in GitHub Desktop.
Save yjbanov/dc817bb344d1c84d60801a6b319e977a to your computer and use it in GitHub Desktop.
diff --git a/lib/web_ui/lib/src/engine/semantics/label_and_value.dart b/lib/web_ui/lib/src/engine/semantics/label_and_value.dart
index 843c4dedda..f5e1bc7780 100644
--- a/lib/web_ui/lib/src/engine/semantics/label_and_value.dart
+++ b/lib/web_ui/lib/src/engine/semantics/label_and_value.dart
@@ -27,7 +27,7 @@ enum LabelRepresentation {
/// role) JAWS on Windows. However, this role is still the most common, as it
/// applies to all container nodes, and many ARIA roles (e.g. checkboxes,
/// radios, scrollables, sliders).
- ariaLabel(AriaLabelRepresentation),
+ ariaLabel,
/// Represents the label as a [DomText] node.
///
@@ -37,7 +37,7 @@ enum LabelRepresentation {
///
/// This representation is compatible with most web crawlers, and it is the
/// best option for certain ARIA roles, such as buttons, links, and headings.
- domText(DomTextRepresentation),
+ domText,
/// Represents the label as a sized span.
///
@@ -45,17 +45,23 @@ enum LabelRepresentation {
/// need to be laid out to compute the right size. It is compatible with most
/// web crawlers, and it is the best options for certain ARIA roles, such as
/// the implicit "generic" role used for plain text (not headings).
- sizedSpan(SizedSpanRepresentation);
-
- const LabelRepresentation(this.implementation);
-
- /// The type used to implement this representation.
- final Type implementation;
+ sizedSpan;
+
+ /// Creates the behavior for this label representation.
+ LabelRepresentationBehavior createBehavior(PrimaryRoleManager owner) {
+ return switch (this) {
+ LabelRepresentation.ariaLabel => AriaLabelRepresentation._(owner),
+ LabelRepresentation.domText => DomTextRepresentation._(owner),
+ LabelRepresentation.sizedSpan => SizedSpanRepresentation._(owner),
+ };
+ }
}
/// Provides a DOM behavior for a [LabelRepresentation].
abstract final class LabelRepresentationBehavior {
- LabelRepresentationBehavior(this.owner);
+ LabelRepresentationBehavior(this.kind, this.owner);
+
+ final LabelRepresentation kind;
/// The role manager that this label representation is attached to.
final PrimaryRoleManager owner;
@@ -103,7 +109,7 @@ abstract final class LabelRepresentationBehavior {
///
/// <flt-semantics aria-label="Hello, World!"></flt-semantics>
final class AriaLabelRepresentation extends LabelRepresentationBehavior {
- AriaLabelRepresentation._(super.owner);
+ AriaLabelRepresentation._(PrimaryRoleManager owner) : super(LabelRepresentation.ariaLabel, owner);
String? _previousLabel;
@@ -137,7 +143,7 @@ final class AriaLabelRepresentation extends LabelRepresentationBehavior {
/// no ARIA role set, or the role does not size the element, then the
/// [SizedSpanRepresentation] representation can be used.
final class DomTextRepresentation extends LabelRepresentationBehavior {
- DomTextRepresentation._(super.owner);
+ DomTextRepresentation._(PrimaryRoleManager owner) : super(LabelRepresentation.domText, owner);
DomText? _domText;
String? _previousLabel;
@@ -200,7 +206,7 @@ typedef _Measurement = ({
/// Text scaling is used to control the size of the screen reader focus ring.
/// This is used for plain text nodes (e.g. paragraphs of text).
final class SizedSpanRepresentation extends LabelRepresentationBehavior {
- SizedSpanRepresentation._(super.owner) {
+ SizedSpanRepresentation._(PrimaryRoleManager owner) : super(LabelRepresentation.sizedSpan, owner) {
_domText.style
// `inline-block` is needed for two reasons:
// - It supports measuring the true size of the text. Pure `block` would
@@ -423,13 +429,9 @@ class LabelAndValue extends RoleManager {
: preferredRepresentation;
LabelRepresentationBehavior? representation = _representation;
- if (representation == null || representation.runtimeType != effectiveRepresentation.implementation) {
+ if (representation == null || representation.kind != effectiveRepresentation) {
representation?.cleanUp();
- _representation = representation = switch (effectiveRepresentation) {
- LabelRepresentation.ariaLabel => AriaLabelRepresentation._(owner),
- LabelRepresentation.domText => DomTextRepresentation._(owner),
- LabelRepresentation.sizedSpan => SizedSpanRepresentation._(owner),
- };
+ _representation = representation = effectiveRepresentation.createBehavior(owner);
}
return representation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment