Skip to content

Commit

Permalink
fix(go-button): pass through aria attributes to native element
Browse files Browse the repository at this point in the history
  • Loading branch information
seanwuapps committed Dec 30, 2023
1 parent 6c8b5de commit 876fdc2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
25 changes: 5 additions & 20 deletions packages/core/src/components/go-button/go-button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, h, Prop, Element, Host, Watch, State, Build } from '@stencil/core';
import { Component, h, Prop, Element, Host, Watch, State } from '@stencil/core';
import { Breakpoints, ColorVariants } from '../../interfaces';
import { $attrs, warning } from '../../utils/helper';

Expand All @@ -13,7 +13,7 @@ import { $attrs, warning } from '../../utils/helper';
shadow: false,
})
export class GoButton {
@Element() root: HTMLElement;
@Element() el: HTMLElement;

/**
* Html type of the button
Expand Down Expand Up @@ -97,34 +97,18 @@ export class GoButton {
componentWillLoad() {
// a11y check
if (this.icon) {
if (!this.root.hasAttribute('aria-label') && !this.root.hasAttribute('aria-labelledby')) {
warning(`go-button with icon must have either aria-label or aria-labelledby attribute`, this.root);
if (!this.el.hasAttribute('aria-label') && !this.el.hasAttribute('aria-labelledby')) {
warning(`go-button with icon must have either aria-label or aria-labelledby attribute`, this.el);
}
}

if (this.block) {
this.handleBlockChange(this.block);
}
this.updateInnerButtonAttributes();

if (Build.isBrowser) {
// watch attribute change in case they're modified by after initial load
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'attributes') {
this.updateInnerButtonAttributes();
}
});
});
observer.observe(this.root, {
attributes: true,
});
}
}

updateInnerButtonAttributes() {
this.inheritedAttributes = {
...this.inheritedAttributes,
...$attrs.bind(this)(true),
};
}
Expand All @@ -147,6 +131,7 @@ export class GoButton {
loading,
loadingAnouncement,
} = this;
console.log(inheritedAttributes);
const Tag = href ? 'a' : 'button';
const rootClasses = `${variant} ${blockClasses}`;
return (
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ export function inheritComponentAttrs(component, excludes: string[] = [], remove
* get attributes that are not defined in a components props, without removing them from the host element
* @returns list of attributes inherited from the host element
*/
export function $attrs(removeAttrs = false) {
export function $attrs(removeAttrs = false, elPropName = 'el') {
const propNames = Object.keys(Object.getPrototypeOf(this));

return inheritAttributes(this.el, ['class', 'style', 'id', ...propNames], removeAttrs);
return inheritAttributes(this[elPropName], ['class', 'style', 'id', 'data-testid', ...propNames], removeAttrs);
}

/**
Expand Down

0 comments on commit 876fdc2

Please sign in to comment.