-
Notifications
You must be signed in to change notification settings - Fork 1
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
VL-11_Add-auto-position-for-datepicers-calendar_Dmytro-Holdobin #50
Merged
itsJohnnyGrid
merged 1 commit into
main
from
VL-11_Add-auto-position-for-datepicers-calendar_Dmytro-Holdobin
Jun 12, 2024
Merged
Changes from all commits
Commits
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 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { ref, computed, toValue } from "vue"; | ||
|
||
export const POSITION = { | ||
left: "left", | ||
right: "right", | ||
top: "top", | ||
bottom: "bottom", | ||
auto: "auto", | ||
}; | ||
|
||
export function useAdjustElementPosition( | ||
anchorElement, | ||
targetElement, | ||
position, | ||
preferredPosition, | ||
) { | ||
const preferredOpenDirectionY = ref(preferredPosition?.y || POSITION.bottom); | ||
const preferredOpenDirectionX = ref(preferredPosition?.x || POSITION.left); | ||
|
||
const localAnchorElement = computed(() => toValue(anchorElement)); | ||
const localTargetElement = computed(() => toValue(targetElement)); | ||
const localPosition = computed(() => toValue(position)); | ||
const localPreferredPosition = computed(() => toValue(preferredPosition)); | ||
|
||
const isTop = computed(() => { | ||
if (localPosition.value.y !== POSITION.auto) { | ||
return localPosition.value.y === POSITION.top; | ||
} | ||
|
||
return preferredOpenDirectionY.value === POSITION.top; | ||
}); | ||
|
||
const isLeft = computed(() => { | ||
if (localPosition.value.x !== POSITION.auto) { | ||
return localPosition.value.x === POSITION.left; | ||
} | ||
|
||
return preferredOpenDirectionX.value === POSITION.right; | ||
}); | ||
|
||
const isBottom = computed(() => { | ||
if (localPosition.value.y !== POSITION.auto) { | ||
return localPosition.value.y === POSITION.bottom; | ||
} | ||
|
||
return preferredOpenDirectionY.value === POSITION.bottom; | ||
}); | ||
|
||
const isRight = computed(() => { | ||
if (localPosition.value.x !== POSITION.auto) { | ||
return localPosition.value.y === POSITION.right; | ||
} | ||
|
||
return preferredOpenDirectionX.value === POSITION.right; | ||
}); | ||
|
||
function adjustPositionY() { | ||
if (typeof window === "undefined") return; | ||
|
||
const spaceAbove = localAnchorElement.value.getBoundingClientRect().top; | ||
const spaceBelow = window.innerHeight - localAnchorElement.value.getBoundingClientRect().bottom; | ||
const hasEnoughSpaceBelow = | ||
spaceBelow > localTargetElement.value.getBoundingClientRect().height; | ||
const hasEnoughSpaceAbove = | ||
spaceAbove > localTargetElement.value.getBoundingClientRect().height; | ||
|
||
if (localPreferredPosition.value.y === POSITION.top) { | ||
preferredOpenDirectionY.value = | ||
hasEnoughSpaceBelow || spaceBelow > spaceAbove ? POSITION.bottom : POSITION.top; | ||
} | ||
|
||
if (localPreferredPosition.value.y === POSITION.bottom) { | ||
preferredOpenDirectionY.value = | ||
hasEnoughSpaceAbove || spaceAbove > spaceBelow ? POSITION.top : POSITION.bottom; | ||
} | ||
} | ||
|
||
function adjustPositionX() { | ||
if (typeof window === "undefined") return; | ||
|
||
const spaceRight = localAnchorElement.value.getBoundingClientRect().right; | ||
const spaceLeft = window.innerWidth - localAnchorElement.value.getBoundingClientRect().left; | ||
const hasEnoughSpaceLeft = spaceLeft > localTargetElement.value.getBoundingClientRect().width; | ||
const hasEnoughSpaceRight = spaceRight > localTargetElement.value.getBoundingClientRect().width; | ||
|
||
if (localPreferredPosition.value.x === POSITION.left) { | ||
preferredOpenDirectionX.value = | ||
hasEnoughSpaceRight || spaceRight > spaceLeft ? POSITION.right : POSITION.left; | ||
} | ||
|
||
if (localPreferredPosition.value.x === POSITION.right) { | ||
preferredOpenDirectionX.value = | ||
hasEnoughSpaceLeft || spaceLeft > spaceRight ? POSITION.left : POSITION.right; | ||
} | ||
} | ||
|
||
return { isTop, isRight, isBottom, isLeft, adjustPositionY, adjustPositionX }; | ||
} |
This file contains 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 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 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 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 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 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
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.
why this is hardcoded, maybe
UIService.get(defaultConfig, UDatePickerRange).default.openDirectionX
?