Skip to content

USWDS - Modal: Implement behaviors and add conditional initialization#5208

Closed
mahoneycm wants to merge 10 commits into
developfrom
cm-modal-behavior
Closed

USWDS - Modal: Implement behaviors and add conditional initialization#5208
mahoneycm wants to merge 10 commits into
developfrom
cm-modal-behavior

Conversation

@mahoneycm

@mahoneycm mahoneycm commented Mar 28, 2023

Copy link
Copy Markdown
Contributor

Summary

Implements behaviors utility and queries DOM before initialization. Now modal follows the pattern set by other dynamically created components by using the behaviors utility. Additionally, the component will query the DOM before building the component to prevent errors caused by multiple initializations being called. This is beneficial for component-level initialization.

Breaking change

This is not a breaking change.

Related issue

Closes #5062

Preview link

Modal (federalist preview)

Test repo (Follow instructions below)

Problem statement

When using modern JS frameworks such as Angular or React, calling on the component to initialize multiple times caused the modal's functionality to break. The error occurred because the initialization process selects all usa-modal divs and rebuilds them using their ID. The newly generated div removes the ID and moves it to the wrapper.

Once initialization is called again, it selects the new usa-modal div without the ID, which causes build errors due to the ID being null

Solution

  1. During initialization, check for previously declared modalIdbefore running setUpModal function

Testing and review

  1. Checkout this PR
  2. Confirm modal js follows standard dynamic component patterns regarding behaviors util
  3. Run npm link
  4. Clone this test repo
  5. Run npm install npm run start
  6. Confirm error on the mulitple modals page
  7. Return to code and run npm link @uswds/uswds
  8. Inspect multiple modal page and confirm modals are working

Notes

Output HTML before change:
image

After:
image

Before opening this PR, make sure you’ve done whichever of these applies to you:

  • Confirm that this code follows the 18F Front End Coding Style Guide and Accessibility Guide.
  • Run git pull origin [base branch] to pull in the most recent updates from your base and check for merge conflicts. (Often, the base branch is develop).
  • Run npm test and confirm that all tests pass.

@mahoneycm mahoneycm requested review from amyleadem and mejiaj March 28, 2023 21:17

@mejiaj mejiaj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for updating this component. Functionality looks good. Added a few comments to potentially improve clarification. Could you also update the PR description? Not seeing links to preview or the test repo mentioned.

I've tested:

  • Code style (I ran prettier)
  • Run npm run test
  • Functionality in StorybookJS

Comment thread packages/usa-modal/src/index.js Outdated
Comment thread packages/usa-modal/src/index.js Outdated
Comment thread packages/usa-modal/src/index.js Outdated
Comment thread packages/usa-modal/src/index.js Outdated
Comment thread packages/usa-modal/src/index.js Outdated
Comment thread packages/usa-modal/src/index.js Outdated
Comment thread packages/usa-modal/src/index.js Outdated

@mejiaj mejiaj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for taking this on! Added a comment and a question.

@mejiaj

mejiaj commented May 2, 2023

Copy link
Copy Markdown
Contributor

@rachidatecs are you able to help us test?

I also noticed that the initialization runs twice in your test repo. So if I have two modals, it'll run init() four times. Any insight as to why?

@rachidatecs

Copy link
Copy Markdown

Happy to pull your branch and test the new code.

As to your question on init running twice in the test repo - that may well be the point of the branch you're on, to demo the issue. Which branch are you running?

@mahoneycm mahoneycm requested a review from mejiaj May 2, 2023 17:46
@mejiaj

mejiaj commented May 2, 2023

Copy link
Copy Markdown
Contributor

@rachidatecs thank you!

For confirming the issue, I was on master in https://github.com/rachidatecs/uswds-modals. I couldn't find why it's firing init double the amount of times it's supposed to.

It makes sense why the first two modals have values and the other two just have null for ID.

@mahoneycm

Copy link
Copy Markdown
Contributor Author

@mejiaj That's a result of the second initialization. When it's called a second time, it selects the rebuilt usa-modal classes and tries to build them again.

The null values are because the new usa-modal element doesn't have an ID like it's placeholder does before initialization

@rachidatecs

rachidatecs commented May 2, 2023

Copy link
Copy Markdown

Yep, that's actually the issue we're trying to solve. We can skirt around it but requiring the package at the page level (the app level will cause modals not on the landing page to not init, and the module level will cause those duplicate inits when more that one modal is present on a page) but that's really a workaround that avoids the issue.

In other words: when the init is called on the component level in a modern js framework (tested in angular and vue, assume it's the same in react), the init method fires every time a modal placeholder is detected in the markup (one init for each modal component on the page). So say you have 3 modals, the first init from the first component will cause all 3 modals to init. The second component will cause all 3 modals to init again. The 3rd component will cause all 3 modals to init a third time. You would end up with 9 generated modals, 3 that are integral and 6 that are missing ids.

@mahoneycm mahoneycm mentioned this pull request Jun 1, 2023
7 tasks
@mahoneycm

Copy link
Copy Markdown
Contributor Author

Closing in favor of #5315

@mahoneycm mahoneycm closed this Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

USWDS - Bug: Bad inits and teardowns on Uswds components when multiple instances in template in modern JS frameworks

3 participants