-
Notifications
You must be signed in to change notification settings - Fork 235
1189/menu removal spike #1198
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
Closed
Closed
1189/menu removal spike #1198
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ import pickerStyles from './picker.css.js'; | |
| import chevronStyles from '@spectrum-web-components/icon/src/spectrum-icon-chevron.css.js'; | ||
|
|
||
| import { Focusable } from '@spectrum-web-components/shared/src/focusable.js'; | ||
| import ElementReparenter from '@spectrum-web-components/shared/src/element-reparenter.js'; | ||
| import '@spectrum-web-components/icon/sp-icon.js'; | ||
| import { Chevron100Icon } from '@spectrum-web-components/icons-ui'; | ||
| import '@spectrum-web-components/icons-workflow/icons/sp-icon-alert.js'; | ||
|
|
@@ -90,6 +91,8 @@ export class PickerBase extends SizedMixin(Focusable) { | |
| @property({ type: Boolean, reflect: true }) | ||
| public open = false; | ||
|
|
||
| private reparentableChildren?: Element[]; | ||
| private elementReparenter?: ElementReparenter; | ||
| public optionsMenu?: Menu; | ||
|
|
||
| /** | ||
|
|
@@ -116,7 +119,6 @@ export class PickerBase extends SizedMixin(Focusable) { | |
|
|
||
| protected listRole = 'listbox'; | ||
| protected itemRole = 'option'; | ||
| private placeholder?: Comment; | ||
|
|
||
| public constructor() { | ||
| super(); | ||
|
|
@@ -233,17 +235,10 @@ export class PickerBase extends SizedMixin(Focusable) { | |
|
|
||
| protected onOverlayClosed(): void { | ||
| this.close(); | ||
| if (this.optionsMenu && this.placeholder) { | ||
| const parentElement = | ||
| this.placeholder.parentElement || | ||
| this.placeholder.getRootNode(); | ||
|
|
||
| if (parentElement) { | ||
| parentElement.replaceChild(this.optionsMenu, this.placeholder); | ||
| } | ||
| if (this.elementReparenter) { | ||
| this.elementReparenter.restore(); | ||
| } | ||
|
|
||
| delete this.placeholder; | ||
| this.elementReparenter = undefined; | ||
|
|
||
| this.menuStateResolver(); | ||
| } | ||
|
|
@@ -253,26 +248,20 @@ export class PickerBase extends SizedMixin(Focusable) { | |
| if ( | ||
| !this.popover || | ||
| !this.optionsMenu || | ||
| this.optionsMenu.children.length === 0 | ||
| !this.reparentableChildren || | ||
| this.reparentableChildren.length === 0 | ||
| ) { | ||
| this.menuStateResolver(); | ||
| return; | ||
| } | ||
|
|
||
| this.placeholder = document.createComment( | ||
| 'placeholder for optionsMenu' | ||
| this.elementReparenter = new ElementReparenter( | ||
| this.reparentableChildren, | ||
| this.optionsMenu | ||
| ); | ||
|
|
||
| this.optionsMenu.selectable = true; | ||
|
|
||
| const parentElement = | ||
| this.optionsMenu.parentElement || this.optionsMenu.getRootNode(); | ||
|
|
||
| if (parentElement) { | ||
| parentElement.replaceChild(this.placeholder, this.optionsMenu); | ||
| } | ||
|
|
||
| this.popover.append(this.optionsMenu); | ||
| this.sizePopover(this.popover); | ||
| const { popover } = this; | ||
| this.closeOverlay = await Picker.openOverlay(this, 'inline', popover, { | ||
|
|
@@ -349,14 +338,17 @@ export class PickerBase extends SizedMixin(Focusable) { | |
| id="popover" | ||
| @click=${this.onClick} | ||
| @sp-overlay-closed=${this.onOverlayClosed} | ||
| ></sp-popover> | ||
| > | ||
| <sp-menu id="menu"></sp-menu> | ||
| </sp-popover> | ||
| `; | ||
| } | ||
|
|
||
| protected firstUpdated(changedProperties: PropertyValues): void { | ||
| super.firstUpdated(changedProperties); | ||
|
|
||
| this.optionsMenu = this.querySelector('sp-menu') as Menu; | ||
| this.optionsMenu = this.shadowRoot.querySelector('sp-menu') as Menu; | ||
| this.reparentableChildren = Array.from(this.children); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we'll want to keep the existing usage backward-compatible for now. So here we'd look for an |
||
| } | ||
|
|
||
| protected updated(changedProperties: PropertyValues): void { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Being you've chosen to make this an object oriented API would it make better sense to recycle the reparenter rather than newing it every time?
I could alternatively see it as a functional API, a la:
I know right, a web component admiring suggesting a functional 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.
In the realm of a class, you will find many parallels with reactive controller approach that the Polymer team is formalizing for the upcoming release of lit-element: https://github.com/Polymer/lit-html/blob/lit-next/packages/reactive-element/CHANGELOG.md
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.
The functional approach definitely seems better here, and I even tried that first. I was passing back state and not a callback though which I didn't like.