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

Add the description for buttons #29

Merged
merged 1 commit into from
Mar 26, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/AOM/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ export class RawNodeAttributes {
@observable "aria-controls"?: HtmlID = undefined;
@observable "aria-disabled"?: string = undefined;
@observable "aria-modal"?: string = undefined;
@observable "aria-description"?: string = undefined;
@observable "aria-describedby"?: HtmlID = undefined;
@observable "aria-haspopup"?: string = undefined;
@observable "aria-expanded"?: string = undefined;
Expand Down Expand Up @@ -838,6 +839,14 @@ export class Aria {
return this.rawAttributes["aria-controls"]?.trim();
}

@computed get ariaDescription() {
return this.rawAttributes["aria-description"]?.trim();
}

@computed get ariaDescribedBy() {
return this.rawAttributes["aria-describedby"]?.trim();
}

@computed get ariaLabel() {
return this.rawAttributes["aria-label"]?.trim();
}
Expand Down Expand Up @@ -1154,6 +1163,18 @@ export class NodeElement {
}
}

get description(): string {
if (this.relations.ariaDescribedBy?.length) {
return getAccessibleNameOf(this.relations.ariaDescribedBy).trim();
}

if (this.attributes.ariaDescription?.trim()) {
return this.attributes.ariaDescription.trim();
}

return "";
}

constructor(node: HTMLElement) {
this.domNode = node;
this.htmlTag = node.tagName.toLowerCase();
Expand Down
9 changes: 9 additions & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {getMap, reconcileFields} from "../AOM/reconcile";

class RelationsForId {
@observable elementsWithId: NonNullable<NodeElement>[] = [];
@observable ariaDescriptions: NonNullable<NodeElement>[] = [];
@observable ariaLabelOf: NonNullable<NodeElement>[] = [];
@observable ariaControlledBy: NonNullable<NodeElement>[] = [];
@observable ariaOwnedBy: NonNullable<NodeElement>[] = [];
Expand Down Expand Up @@ -298,6 +299,14 @@ export default class Store {
}
}

this.updateSingleReferenceRelation(
node,
oldAttributes?.ariaDescribedBy,
newAttributes?.ariaDescribedBy,
"ariaDescribedBy",
"ariaDescriptions"
);

this.updateSingleReferenceRelation(
node,
oldAttributes?.ariaLabelledBy,
Expand Down
14 changes: 12 additions & 2 deletions src/view/renderers/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,29 @@ const SimpleButtonRole = styled.span<{isSelected:boolean}>`
${props => props.isSelected && selectedBoxShadow};
`;

const SimpleButtonContent = styled.span<{ isHovered: boolean }>`
const SimpleButtonContent = styled.span<{ hasDescription: boolean, isHovered: boolean }>`
display: inline-block;
padding: 0 5px;
background: transparent;
min-width: 100px;
cursor: pointer;
line-height: 1.33;

border-radius: 0 ${borderRadius} ${borderRadius} 0;
border: ${props => (props.isHovered ? `1px solid ${color}` : ` 1px dashed #555`)};

:hover {
background: #555;
}

${props => props.hasDescription && `::after { content: "🛈" }`};
`;

const SimpleButtonDescription = styled.span<{}>`
`;

const SimpleButton = observer(function SimpleButton({ node }: ComponentProps) {
const description = node.description
const [isHovered, setHovered] = React.useState(false);
const [ref, style] = useFocusable(node);
const roleRef = useRef<HTMLSpanElement>(null);
Expand All @@ -76,7 +83,10 @@ const SimpleButton = observer(function SimpleButton({ node }: ComponentProps) {
isSelected={node?.isOpenInSidePanel}>
🖱️
</SimpleButtonRole>
<SimpleButtonContent isHovered={isHovered}>{node.accessibleName}&nbsp;</SimpleButtonContent>
<SimpleButtonContent
isHovered={isHovered} title={description} hasDescription={!!description && !node.isFocused}>
{node.accessibleName}&nbsp;{node.isFocused && description && `- ${description}`}
</SimpleButtonContent>
</SimpleButtonWrapper>
);
});
Expand Down