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

feat(sp-button-group): sp-button-group react to size updates #5037

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 20 additions & 1 deletion packages/button-group/src/ButtonGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ governing permissions and limitations under the License.
import {
CSSResultArray,
html,
PropertyValues,
SizedMixin,
SpectrumElement,
TemplateResult,
} from '@spectrum-web-components/base';
import { property } from '@spectrum-web-components/base/src/decorators.js';
import {
property,
query,
} from '@spectrum-web-components/base/src/decorators.js';
import type { Button } from '@spectrum-web-components/button';

import styles from './button-group.css.js';
Expand All @@ -36,9 +40,24 @@ export class ButtonGroup extends SizedMixin(SpectrumElement, {
@property({ type: Boolean, reflect: true })
public vertical = false;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in the scope of this PR, but interestingly, I just noticed that when we extend the SizedMixin, the size property doesn’t appear in our documentation 😶‍🌫️.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did too, I had to look in the code to see if this component actually supports a size attribute

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it stands, strictly speaking, this is not part of our official API because we don’t explicitly document it, even though it seems it was always intended to be included 😅 .
I’ll sync with our team briefly to decide how we want to address this - it shouldn’t impact this PR, but I’ll report back if it does.

@query('slot')
slotElement!: HTMLSlotElement;

protected override updated(changedProperties: PropertyValues): void {
super.updated(changedProperties);

if (changedProperties.has('size')) {
this.manageChildrenSize(this.slotElement);
}
}

protected handleSlotchange({
target: slot,
}: Event & { target: HTMLSlotElement }): void {
this.manageChildrenSize(slot);
}

private manageChildrenSize(slot: HTMLSlotElement): void {
const assignedElements = slot.assignedElements() as Button[];
assignedElements.forEach((button) => {
button.size = this.size;
Expand Down
17 changes: 17 additions & 0 deletions packages/button-group/test/button-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,21 @@ describe('Buttongroup', () => {

await expect(el).to.be.accessible();
});
it(`manages its children's size`, async () => {
rubencarvalho marked this conversation as resolved.
Show resolved Hide resolved
const el = await fixture<ButtonGroup>(buttons(buttons.args));
await elementUpdated(el);

let children = el.querySelectorAll('sp-button');
children.forEach((button) => {
expect(button.size).to.equal('m');
});

el.size = 's';
await elementUpdated(el);

children = el.querySelectorAll('sp-button');
children.forEach((button) => {
expect(button.size).to.equal('s');
});
});
});
Loading