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): add hasMatchingState and swapStates functions #33156

Merged
merged 8 commits into from
Oct 30, 2024

Conversation

radium-v
Copy link
Contributor

Previous Behavior

For each instance where we're toggling between two states, we remove the previous state if it exists, and then apply the new state if it exists.

There are no checks to see if the new state getting checked is an allowed state for the attribute. This allows for arbitrary states to be applied, which can lead to unexpected behavior. For instance, an appearance attribute could be set to any value, and that value would be applied to ElementInternals.states.

It could also lead to state collisions, where setting something like appearance="large" would simultaneously be invalid ("large" is not a valid state for appearance), and would apply the large state to the ElementInternals.states set (large is a valid state for the size attribute, but the size attribute isn't set to large.)

New Behavior

This PR adds the hasMatchingState function, which can be used to determine if a given string state is valid and accepted for a given state object.

This PR also adds the swapStates function, which can be used to swap between two states. This is a really common pattern in our codebase, and this function will make it easier to remove the old state, and apply the new state, while also ensuring that the new state is accepted for that attribute.

@radium-v radium-v requested a review from a team as a code owner October 28, 2024 22:48
@radium-v radium-v force-pushed the users/radium-v/wc-guard-states branch from 321b1f4 to 09feacb Compare October 29, 2024 20:11
@fabricteam
Copy link
Collaborator

fabricteam commented Oct 29, 2024

📊 Bundle size report

✅ No changes found

@radium-v radium-v merged commit 36474e5 into microsoft:master Oct 30, 2024
16 checks passed
@radium-v radium-v deleted the users/radium-v/wc-guard-states branch October 30, 2024 01:20
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.

4 participants