-
Notifications
You must be signed in to change notification settings - Fork 77
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
refactor(dom): Update getSlotted utility to handle querying for item(s) in default slot #3788
Conversation
…s) in the default slot.
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.
This is looking great! This will be a nice update to getSlotted
. 🎉
@@ -185,7 +187,7 @@ function querySingle<T extends Element = Element>( | |||
match = options && options.direct === false ? match : match?.parentElement === element ? match : null; | |||
|
|||
const selector = options?.selector; | |||
return selector ? match.querySelector<T>(selector) : match; | |||
return selector ? match?.querySelector<T>(selector) : match; |
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.
Since the default slot selector will also return children in a custom element without a <slot>
, this should also make sure that assignedSlot
from the default slot selector result is not 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.
I don't think the assignedSlot
will work on the spec test. I think we would have to setup an e2e test?
src/utils/dom.ts
Outdated
options?: GetSlottedOptions | ||
): T | null; | ||
export function getSlotted<T extends Element = Element>( | ||
element: Element, | ||
slotName: string, | ||
slotName?: string, |
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.
Can you update the signature to support both
getSlotted(el, slotName, options);
getSlotted(el, options);
?
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.
Yeah, or we can just update the slotName to be apart of options? What do you think?
Do you have an example of making the second param be identified as the third?
# Conflicts: # src/utils/dom.spec.ts # src/utils/dom.ts
@jcfranco can you review again? |
@@ -196,6 +205,10 @@ function queryMultiple<T extends Element = Element>( | |||
let matches = Array.from(element.querySelectorAll<T>(slotSelector)); | |||
matches = options && options.direct === false ? matches : matches.filter((el) => el.parentElement === element); | |||
|
|||
if (slotSelector === defaultSlotSelector) { | |||
matches = matches.filter((match) => match?.assignedSlot); |
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.
@jcfranco It seems like this would work but we would need to move the spec tests to e2e for assignedSlot to work I think.
This PR has been automatically marked as stale because it has not had recent activity. Please close your PR if it is no longer relevant. Thank you for your contributions. |
<calcite-dropdown> | ||
<calcite-action slot="dropdown-trigger">Open dropdown</calcite-button> | ||
<calcite-action slot="dropdown-trigger">Open dropdown</calcite-action> |
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 missed this in review :( cc @Elijbet
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.
Code changes LGTM!
For the remaining items I brought up (1. getSlotted
signature, 2. assignedSlot
check), can you create follow up issues?
</slot-test> | ||
`; | ||
|
||
const assignedSlot = document.querySelector("slot-test").shadowRoot.querySelector(`slot:not([name])`); |
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 this a workaround for Node.assignedSlot
missing? If so, just wanted to check if it's working or not based on your comments to move to an e2e test.
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.
Yes, this is a workaround.
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.
Without this, the assignedNode won't be there. So we would have to run the getSlotted() method in the browser. Which means placing the whole utility in the browser so that all methods can be accessed.
@@ -162,24 +162,33 @@ interface GetSlottedOptions { | |||
selector?: string; | |||
} | |||
|
|||
const defaultSlotSelector = "> :not([slot])"; | |||
|
|||
export function getSlotted<T extends Element = Element>( |
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 think this should be typed like so:
export function getSlotted<T extends Element = Element>(element: Element, options: GetSlottedOptions & { all: true }): T[];
export function getSlotted<T extends Element = Element>(element: Element, options?: GetSlottedOptions): T[];
export function getSlotted<T extends Element = Element>(element: Element, slotName: string | string[], options: GetSlottedOptions & { all: true }): T[];
export function getSlotted<T extends Element = Element>(element: Element, slotName: string | string[], options?: GetSlottedOptions): T[];
export function getSlotted<T extends Element = Element>(
element: Element,
slotNameOrOptions: string | string[] | GetSlottedOptions,
optionsOrUndefined?: GetSlottedOptions): (T | null) | T[] { /* ... */ }
Can you create a separate refactor issue this one and assign it to me?
Related Issue: None
Summary
refactor(dom): Update getSlotted utility to handle querying for item(s) in default slot
getSlotted
to query for an element in the default slot.slotName
param optionalThis will allow us to replace
this.hasDefaultSlot = this.el.querySelector(":not([slot])") !== null;
taken fromcombobox-item
.