-
Notifications
You must be signed in to change notification settings - Fork 2
feat(WW-4125): bindable position + auto vertical/horizontal opening direction #18
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
Open
kevinbarfleur
wants to merge
2
commits into
main
Choose a base branch
from
WW-4125_dropdown-auto-direction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,6 +93,8 @@ export default { | |
| }); | ||
|
|
||
| const triggerBox = ref({}); | ||
| const resolvedPosition = ref(props.content.position); | ||
|
|
||
| const synchronizeTriggerBox = () => { | ||
| if (!triggerElementRef?.value) return; | ||
| const box = triggerElementRef.value.getBoundingClientRect(); | ||
|
|
@@ -104,6 +106,95 @@ export default { | |
| width: box.width, | ||
| height: box.height, | ||
| }; | ||
| computeResolvedPosition(); | ||
| }; | ||
|
|
||
| const computeResolvedPosition = () => { | ||
| const position = props.content.position; | ||
| const box = triggerBox.value; | ||
| if (!box || box.width === undefined) { | ||
| resolvedPosition.value = position; | ||
| return; | ||
| } | ||
|
|
||
| const frontWindow = wwLib.getFrontWindow(); | ||
| const viewportHeight = frontWindow.innerHeight; | ||
| const viewportWidth = frontWindow.innerWidth; | ||
|
|
||
| const dropdownElement = dropdownElementRef?.value; | ||
| if (!dropdownElement) { | ||
| resolvedPosition.value = position; | ||
| return; | ||
| } | ||
|
|
||
| const dropdownHeight = dropdownElement.offsetHeight || box.height; | ||
| const dropdownWidth = dropdownElement.offsetWidth || box.width; | ||
| const computedStyle = frontWindow.getComputedStyle(dropdownElement); | ||
| const renderedPosition = resolvedPosition.value; | ||
|
|
||
| const getRenderedOffset = (property, reference) => { | ||
| const value = Number.parseFloat(computedStyle[property]); | ||
| return Number.isFinite(value) ? value - reference : 0; | ||
| }; | ||
|
|
||
| let offsetY = 0; | ||
| let offsetX = 0; | ||
| switch (renderedPosition) { | ||
| case "top": | ||
| offsetY = getRenderedOffset("bottom", viewportHeight - box.top); | ||
| break; | ||
| case "bottom": | ||
| offsetY = getRenderedOffset("top", box.bottom); | ||
| break; | ||
| case "left": | ||
| offsetX = getRenderedOffset("right", viewportWidth - box.left); | ||
| break; | ||
| case "right": | ||
| offsetX = getRenderedOffset("left", box.right); | ||
| break; | ||
| } | ||
|
|
||
| if ( | ||
| props.content.autoVertical && | ||
| (position === "top" || position === "bottom") | ||
| ) { | ||
| const spaceBelow = viewportHeight - box.bottom - offsetY; | ||
| const spaceAbove = box.top - offsetY; | ||
| const fitsBelow = spaceBelow >= dropdownHeight; | ||
| const fitsAbove = spaceAbove >= dropdownHeight; | ||
| if (!fitsBelow && (fitsAbove || spaceAbove > spaceBelow)) { | ||
| resolvedPosition.value = "top"; | ||
| } else { | ||
| resolvedPosition.value = "bottom"; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| props.content.autoHorizontal && | ||
| (position === "left" || position === "right") | ||
| ) { | ||
| const spaceRight = viewportWidth - box.right - offsetX; | ||
| const spaceLeft = box.left - offsetX; | ||
| const fitsRight = spaceRight >= dropdownWidth; | ||
| const fitsLeft = spaceLeft >= dropdownWidth; | ||
| if (position === "right") { | ||
| if (!fitsRight && (fitsLeft || spaceLeft > spaceRight)) { | ||
| resolvedPosition.value = "left"; | ||
| } else { | ||
| resolvedPosition.value = "right"; | ||
| } | ||
| } else { | ||
| if (!fitsLeft && (fitsRight || spaceRight > spaceLeft)) { | ||
| resolvedPosition.value = "right"; | ||
| } else { | ||
| resolvedPosition.value = "left"; | ||
| } | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| resolvedPosition.value = position; | ||
| }; | ||
|
|
||
| function onWindowClick(event) { | ||
|
|
@@ -153,15 +244,17 @@ export default { | |
| scrollableParents.push(wwLib.getFrontWindow()); | ||
| } | ||
|
|
||
| function observeDropdownSize() { | ||
| if (!resizeObserver || !dropdownElementRef.value) return; | ||
| resizeObserver.observe(dropdownElementRef.value); | ||
| } | ||
|
|
||
| function startPositioningDropdown() { | ||
| synchronizeTriggerBox(); | ||
| wwLib.getFrontDocument().addEventListener("click", onWindowClick); | ||
| resizeObserver = new ResizeObserver((entries) => { | ||
| const entry = entries[0]; | ||
| triggerBox.value.width = entry.contentRect.width; | ||
| triggerBox.value.height = entry.contentRect.height; | ||
| }); | ||
| resizeObserver = new ResizeObserver(synchronizeTriggerBox); | ||
| resizeObserver.observe(triggerElementRef.value); | ||
| observeDropdownSize(); | ||
| setScrollableParents(triggerElementRef.value); | ||
| scrollableParents.forEach((p) => { | ||
| p.addEventListener("scroll", synchronizeTriggerBox, { passive: true }); | ||
|
|
@@ -174,6 +267,7 @@ export default { | |
| function stopPositioningDropdown() { | ||
| wwLib.getFrontDocument().removeEventListener("click", onWindowClick); | ||
| resizeObserver?.disconnect(); | ||
| resizeObserver = null; | ||
| scrollableParents.forEach((p) => { | ||
| p.removeEventListener("scroll", synchronizeTriggerBox); | ||
| wwLib | ||
|
|
@@ -189,6 +283,11 @@ export default { | |
| delayedIsClosed.value = false; | ||
| nextTick(() => { | ||
| delayedIsOpen.value = true; | ||
| nextTick(() => { | ||
| if (!isDisplayed.value) return; | ||
| observeDropdownSize(); | ||
| computeResolvedPosition(); | ||
| }); | ||
| }); | ||
| } else { | ||
| stopPositioningDropdown(); | ||
|
|
@@ -221,10 +320,27 @@ export default { | |
| } | ||
| ); | ||
|
|
||
| watch( | ||
| () => [ | ||
| props.content.position, | ||
| props.content.autoVertical, | ||
| props.content.autoHorizontal, | ||
| props.content.offsetX, | ||
| props.content.offsetY, | ||
| ], | ||
| ([position], [previousPosition]) => { | ||
| if (position !== previousPosition) { | ||
| resolvedPosition.value = position; | ||
| } | ||
| nextTick(() => computeResolvedPosition()); | ||
| } | ||
| ); | ||
|
Comment on lines
+323
to
+337
Contributor
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. Est-ce qu’on devrait aussi relancer le calcul quand |
||
|
|
||
| return { | ||
| appDiv, | ||
| synchronizeTriggerBox, | ||
| triggerBox, | ||
| resolvedPosition, | ||
| isOpened, | ||
| timeoutId, | ||
| isEditing, | ||
|
|
@@ -237,7 +353,7 @@ export default { | |
| computed: { | ||
| style() { | ||
| const style = {}; | ||
| const position = this.content.position; | ||
| const position = this.resolvedPosition; | ||
| const alignment = this.content.alignment; | ||
|
|
||
| const offsetX = | ||
|
|
||
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
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.
Est-ce qu’on devrait plutôt se baser sur les dimensions réellement rendues du dropdown ? Avec
parseFloat, un offset comme10%est interprété comme10pxet une variable CSS comme0, ce qui peut choisir la mauvaise direction.