-
Notifications
You must be signed in to change notification settings - Fork 6.8k
refactor(platform): consolidate and expose shadow dom detection utilities #18582
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
Conversation
430d397
to
cafafbb
Compare
0b72e73
to
9bd9660
Compare
if (shadowDomIsSupported == null) { | ||
const head = typeof document !== 'undefined' ? document.head : null; | ||
shadowDomIsSupported = !!(head && ((head as any).createShadowRoot || head.attachShadow)); | ||
} | ||
|
||
return shadowDomIsSupported; | ||
} | ||
|
||
/** Gets the shadow root of an element, if supported and the element is inside the Shadow DOM. */ | ||
export function getShadowRoot(element: HTMLElement): Node | null { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is cdk/platform the right place for this? I'm not sure we have a package that really makes sense for it currently. I would think something like cdk/dom would be the right place
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally could see it live in something like cdk/platform/dom
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm extremely hesitant to get into the business of general DOM helper APIs. I think it's a slippery slope in terms of adding more, and they tend to go out of date over the years. I'm okay with this living here if we keep it as a private API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not really private if we export it at the top level though. Also we're already in the business of detecting other DOM APIs like supported input types and scroll behavior so I don't see how detecting if the Shadow DOM is supported is any different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a cdk/testing/private
, maybe we could make cdk/platform/private
(I'm less concerned about it being in this package if its private)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be fine with putting it under private
. The main concern I'm trying to address with these changes is the code duplication.
changing to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general LGTM (except for the question on where to put this)
4a42bfc
to
a333883
Compare
…ties Over time we had accumulated the same utility for finding the shadow root of an element in multiple places. These changes move it into a common place under `cdk/platform`. I also decided to remove the underscore from `_supportsShadowDom` so it's officially a public API. It was already exposed under `cdk/platform` so there was nothing stopping people from using it, but the underscore looked weird. The API should be stable enough, since we've had it for more than a year in multiple components and we haven't had issues with it.
a333883
to
4ad46c0
Compare
…ties (#18582) Over time we had accumulated the same utility for finding the shadow root of an element in multiple places. These changes move it into a common place under `cdk/platform`. I also decided to remove the underscore from `_supportsShadowDom` so it's officially a public API. It was already exposed under `cdk/platform` so there was nothing stopping people from using it, but the underscore looked weird. The API should be stable enough, since we've had it for more than a year in multiple components and we haven't had issues with it. (cherry picked from commit fa96c8a)
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Over time we had accumulated the same utility for finding the shadow root of an element in multiple places. These changes move it into a common place under
cdk/platform
.Note: I'm setting this to a P2, because it's required to make more shadow DOM-related fixes without duplicating the logic.