-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react): useScrollTo align 옵션 추가 (#381)
- Loading branch information
Showing
5 changed files
with
152 additions
and
36 deletions.
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,5 @@ | ||
--- | ||
'@modern-kit/react': patch | ||
--- | ||
|
||
fix(react): useScrollTo align 옵션 추가 - @ssi02014 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,73 @@ | ||
import { isWindow } from '@modern-kit/utils'; | ||
|
||
type Align = 'start' | 'center' | 'end'; | ||
|
||
export interface ScrollToElementOptions { | ||
offsetX?: number; | ||
offsetY?: number; | ||
behavior?: ScrollBehavior; | ||
alignY?: Align; // 수직 | ||
alignX?: Align; // 수평 | ||
} | ||
|
||
const getAlignPosition = ( | ||
containerDimension: number, | ||
targetDimension: number, | ||
align: Align | ||
) => { | ||
if (align === 'start') { | ||
return 0; | ||
} | ||
if (align === 'center') { | ||
return -containerDimension / 2 + targetDimension / 2; | ||
} | ||
return -containerDimension + targetDimension; | ||
}; | ||
|
||
/** | ||
* @description parent가 window인지 특정 요소인지에 child의 위치 값을 가져오는 함수 | ||
*/ | ||
export const getRelativePosition = <T extends HTMLElement>( | ||
parent: T | Window, | ||
child: HTMLElement, | ||
container: T | Window, | ||
target: HTMLElement, | ||
scrollToOptions: ScrollToElementOptions | ||
) => { | ||
const childRect = child.getBoundingClientRect(); | ||
const { offsetX = 0, offsetY = 0 } = scrollToOptions; | ||
const targetRect = target.getBoundingClientRect(); | ||
const { | ||
offsetX = 0, | ||
offsetY = 0, | ||
alignY = 'start', | ||
alignX = 'start', | ||
} = scrollToOptions; | ||
|
||
if (isWindow(parent)) { | ||
if (isWindow(container)) { | ||
return { | ||
top: childRect.top + window.scrollY + offsetY, | ||
left: childRect.left + window.scrollX + offsetX, | ||
top: | ||
targetRect.top + | ||
window.scrollY + | ||
offsetY + | ||
getAlignPosition(window.innerHeight, targetRect.height, alignY), | ||
left: | ||
targetRect.left + | ||
window.scrollX + | ||
offsetX + | ||
getAlignPosition(window.innerWidth, targetRect.width, alignX), | ||
}; | ||
} | ||
const parentRect = parent.getBoundingClientRect(); | ||
const containerRect = container.getBoundingClientRect(); | ||
|
||
return { | ||
top: childRect.top - parentRect.top + parent.scrollTop + offsetY, | ||
left: childRect.left - parentRect.left + parent.scrollLeft + offsetX, | ||
top: | ||
targetRect.top - | ||
containerRect.top + | ||
container.scrollTop + | ||
offsetY + | ||
getAlignPosition(containerRect.height, targetRect.height, alignY), | ||
left: | ||
targetRect.left - | ||
containerRect.left + | ||
container.scrollLeft + | ||
offsetX + | ||
getAlignPosition(containerRect.width, targetRect.width, alignX), | ||
}; | ||
}; |
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