Skip to content

Use the Shadow DOM for required HTML and CSS. #38

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

Merged
merged 12 commits into from
Apr 16, 2021
Merged

Use the Shadow DOM for required HTML and CSS. #38

merged 12 commits into from
Apr 16, 2021

Conversation

koddsson
Copy link
Contributor

@koddsson koddsson commented Apr 8, 2021

This PR creates a Shadow DOM for <image-crop>.

I moved the required HTML structure and all the CSS necessary for the component into the Shadow DOM. This change means that consumers don't need to integrate the CSS when they want to use this component.

This change doesn't change functionality in any way but will be a breaking change since we have deleted the required CSS file, and consumers of the element no longer need to reference it.

References

https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM
https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_templates_and_slots
https://developer.mozilla.org/en-US/docs/Web/CSS/:host()
https://developer.mozilla.org/en-US/docs/Web/CSS/::slotted

@koddsson koddsson requested a review from a team as a code owner April 8, 2021 14:20
@koddsson koddsson requested review from bscofield and T-Hugs April 8, 2021 14:20
@koddsson koddsson closed this Apr 8, 2021
@koddsson koddsson reopened this Apr 8, 2021
@koddsson koddsson marked this pull request as draft April 8, 2021 14:20
@koddsson
Copy link
Contributor Author

koddsson commented Apr 8, 2021

This was supposed to be a draft!

@koddsson koddsson changed the title Shadowdom Use the Shadow DOM for required HTML and CSS. Apr 8, 2021
@koddsson koddsson requested a review from keithamus April 8, 2021 16:08
@koddsson koddsson marked this pull request as ready for review April 8, 2021 16:08
Comment on lines +95 to +99
function getShadowHost(el: HTMLElement) {
const rootNode = el.getRootNode()
if (!(rootNode instanceof ShadowRoot)) return el
return rootNode.host
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be good if we could avoid this function, but I haven't been able to find an alternative that allows me to access the Shadow DOM host. Open to ideas; otherwise, we can keep this as-is for now.

Copy link

Choose a reason for hiding this comment

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

You could inline it with something like el.getRootNode()?.host ?? el

Copy link

@T-Hugs T-Hugs left a comment

Choose a reason for hiding this comment

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

🚀

Copy link
Contributor

@keithamus keithamus left a comment

Choose a reason for hiding this comment

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

This LGTM! Do you foresee any problems integrating this? Anyone who would have customised the provided CSS, how do they now do that? Perhaps that's something to document in the README?

@koddsson
Copy link
Contributor Author

Do you foresee any problems integrating this?

I don't foresee a lot of problems with integrating this change. I am mostly expecting browser support and compatibility issues, but I've done some due diligence with browsers, and the default behavior seems to work fine to me.

Anyone who would have customised the provided CSS, how do they now do that?

Systems that do customize the HTML that now exists in the Shadow DOM will have issues, but I want to ship this change and gather feedback to see if there are issues we need addressing. I'm assuming that most applications use the provided CSS.

If this turns out to be an issue, other applications can stay on the previous version while working on solutions. We can probably solve some of them using the ::part selector to expose some of the Shadow DOM to the encompassing application. The declarative Shadow DOM might also be helpful in this situation when it has gained more browser support.

Perhaps that's something to document in the README?

Excellent shout; I'll add these new restrictions to the README.

@koddsson
Copy link
Contributor Author

Systems that do customize the HTML that now exists in the Shadow DOM will have issues, but I want to ship this change and gather feedback to see if there are issues we need addressing. I'm assuming that most applications use the provided CSS.

I spoke too soon; we are customizing the CSS on github.com. 😂

@koddsson
Copy link
Contributor Author

github.com adds custom CSS to round the crop mask. I feel like this is still something that the component can provide, so I added a rounded attribute that will make those visual changes to the mask in the Shadow DOM.

@koddsson koddsson merged commit e2b2e2e into main Apr 16, 2021
@koddsson koddsson deleted the shadowdom branch April 16, 2021 10:03
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.

3 participants