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

chore(web-components): use predicate function instead of instanceof in Accordion #32822

Conversation

radium-v
Copy link
Contributor

Previous Behavior

In systems which bundle components individually, we noticed that by using instanceof in the Accordion class, we're pulling the BaseAccordion class into our bundles.

New Behavior

This PR introduces type predicate functions, isAccordionItem and isAccordion, to cheaply determine if a child component is one which is expected. These functions do this by checking if the tagName ends with a baseName value, so isAccordionItem(element) will return true if element.tagName ends with "accordion-item".

Some other changes:

  • The top-level index.ts exports accordionDefinition and accordionItemDefinition. These should be deprecated and removed in favor of the more consistent PascalCased AccordionDefinition and AccordionItemDefinition.
  • The Accordion.accordionItems property was typed as Element[], which required a lot of checking to confirm if the collection actually contains AccordionItem[]. This PR fixes the property's type to make it easier to work with.

This PR also fixes some minor API Extractor warnings generated by the Accordion and AccordionItem modules.

@radium-v radium-v requested a review from a team as a code owner September 12, 2024 16:38
@fabricteam
Copy link
Collaborator

fabricteam commented Sep 12, 2024

📊 Bundle size report

✅ No changes found

@fabricteam
Copy link
Collaborator

fabricteam commented Sep 12, 2024

🕵 fluentui-web-components-v3 No visual regressions between this PR and main

@radium-v radium-v force-pushed the users/radium-v/accordion-item-type-predicate branch from 7c2686d to 23d7954 Compare September 13, 2024 00:59
@radium-v
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 4 pipeline(s).

*
* @public
*/
export function isAccordion(element: Element): element is Accordion {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should element be HTMLElement instead?

Comment on lines 8 to +9
export { styles as accordionStyles } from './accordion.styles.js';
export { definition as accordionDefinition } from './accordion.definition.js';
export { template as accordionTemplate } from './accordion.template.js';
Copy link
Contributor

Choose a reason for hiding this comment

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

Capitalize AccordionStyles and AccordionTemplate - Also in PR #32075

(item: Element | BaseAccordionItem) => item instanceof BaseAccordionItem && item.expanded,
) ?? this.accordionItems[0]
);
private findExpandedItem(): BaseAccordionItem | null {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think based on the changes this can't return null anymore, only undefined?

This pull request has been automatically marked as stale because it was marked as requiring author feedback but has not had any activity for 7 days. It will be closed if no further activity occurs within 5 days of this comment. Thank you for your contributions to Fluent UI!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants