-
Notifications
You must be signed in to change notification settings - Fork 4
feat(dropdown): added safe triangle for submenu (#DS-2446) #1906
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
NikGurev
wants to merge
6
commits into
main
Choose a base branch
from
feat/DS-2446
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 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8b71b23
feat(dropdown): added safe triangle for submenu (#DS-2446)
NikGurev c2bfbbc
fix: after review
NikGurev ea516e5
chore: after review
NikGurev 6e20872
fix: open panel properly on sibling item hover
NikGurev 0d77556
feat: refactor
NikGurev b216363
fix: prettier
NikGurev 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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { KbqTriangle, getSafeTriangleVertices, isPointInRect, isPointInTriangle } from './safe-triangle'; | ||
|
|
||
| const rect = (left: number, top: number, right: number, bottom: number): DOMRect => | ||
| ({ left, top, right, bottom, width: right - left, height: bottom - top, x: left, y: top }) as DOMRect; | ||
|
|
||
| describe('isPointInRect', () => { | ||
| const target = rect(100, 100, 200, 200); | ||
|
|
||
| it('should be true for a point inside the rect', () => { | ||
| expect(isPointInRect({ x: 150, y: 150 }, target)).toBe(true); | ||
| }); | ||
|
|
||
| it('should be true for a point exactly on an edge', () => { | ||
| expect(isPointInRect({ x: 100, y: 150 }, target)).toBe(true); | ||
| expect(isPointInRect({ x: 200, y: 150 }, target)).toBe(true); | ||
| }); | ||
|
|
||
| it('should be false for a point outside the rect', () => { | ||
| expect(isPointInRect({ x: 50, y: 50 }, target)).toBe(false); | ||
| expect(isPointInRect({ x: 250, y: 150 }, target)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isPointInTriangle', () => { | ||
| const triangle: KbqTriangle = { a: { x: 0, y: 0 }, b: { x: 100, y: 0 }, c: { x: 0, y: 100 } }; | ||
|
|
||
| it('should be true for a point inside the triangle', () => { | ||
| expect(isPointInTriangle({ x: 10, y: 10 }, triangle)).toBe(true); | ||
| }); | ||
|
|
||
| it('should be true for a point exactly on an edge', () => { | ||
| expect(isPointInTriangle({ x: 50, y: 0 }, triangle)).toBe(true); | ||
| }); | ||
|
|
||
| it('should be true for a vertex', () => { | ||
| expect(isPointInTriangle(triangle.a, triangle)).toBe(true); | ||
| }); | ||
|
|
||
| it('should be false for a point outside the triangle', () => { | ||
| expect(isPointInTriangle({ x: 60, y: 60 }, triangle)).toBe(false); | ||
| expect(isPointInTriangle({ x: -10, y: -10 }, triangle)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getSafeTriangleVertices', () => { | ||
| it('should use the left edge when the submenu opens to the right of the origin', () => { | ||
| const origin = { x: 90, y: 50 }; | ||
| const target = rect(100, 0, 300, 200); | ||
|
|
||
| expect(getSafeTriangleVertices(origin, target)).toEqual({ | ||
| a: origin, | ||
| b: { x: 100, y: 0 }, | ||
| c: { x: 100, y: 200 } | ||
| }); | ||
| }); | ||
|
|
||
| it('should use the right edge when the submenu opens to the left of the origin', () => { | ||
| const origin = { x: 310, y: 50 }; | ||
| const target = rect(0, 0, 300, 200); | ||
|
|
||
| expect(getSafeTriangleVertices(origin, target)).toEqual({ | ||
| a: origin, | ||
| b: { x: 300, y: 0 }, | ||
| c: { x: 300, y: 200 } | ||
| }); | ||
| }); | ||
|
|
||
| it('should pick the nearer edge when the origin is directly above the panel', () => { | ||
| const target = rect(0, 100, 200, 300); | ||
|
|
||
| expect(getSafeTriangleVertices({ x: 190, y: 50 }, target).b).toEqual({ x: 200, y: 100 }); | ||
| expect(getSafeTriangleVertices({ x: 10, y: 50 }, target).b).toEqual({ x: 0, y: 100 }); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /** | ||
| * A simple (x, y) coordinate. Picked from the DOM's own `DOMPointReadOnly` rather than hand-rolled, so | ||
| * a plain `{ x, y }` literal (e.g. from a `MouseEvent`) satisfies it without constructing a `DOMPoint` — | ||
| * `DOMPoint` isn't implemented in every runtime (e.g. jsdom). | ||
| * @docs-private | ||
| */ | ||
| export type KbqPoint = Pick<DOMPointReadOnly, 'x' | 'y'>; | ||
|
|
||
| /** | ||
| * A triangle described by its three vertices. | ||
| * @docs-private | ||
| */ | ||
| export interface KbqTriangle { | ||
| a: KbqPoint; | ||
| b: KbqPoint; | ||
| c: KbqPoint; | ||
| } | ||
|
|
||
| /** | ||
| * Whether `point` lies within (or on the edge of) `rect`. | ||
| * @docs-private | ||
| */ | ||
| export function isPointInRect(point: KbqPoint, rect: DOMRect): boolean { | ||
| return point.x >= rect.left && point.x <= rect.right && point.y >= rect.top && point.y <= rect.bottom; | ||
| } | ||
|
|
||
| /** | ||
| * Whether `point` lies within (or on the edge of) `triangle`. | ||
| * | ||
| * Uses the sign of the cross product of each triangle edge with the point: the point is inside | ||
| * only if it's consistently on the same side of all three edges. | ||
| * @docs-private | ||
| */ | ||
| export function isPointInTriangle(point: KbqPoint, triangle: KbqTriangle): boolean { | ||
| const { a, b, c } = triangle; | ||
|
|
||
| const sign = (p1: KbqPoint, p2: KbqPoint, p3: KbqPoint): number => | ||
| (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y); | ||
|
|
||
| const d1 = sign(point, a, b); | ||
| const d2 = sign(point, b, c); | ||
| const d3 = sign(point, c, a); | ||
|
|
||
| const hasNegative = d1 < 0 || d2 < 0 || d3 < 0; | ||
| const hasPositive = d1 > 0 || d2 > 0 || d3 > 0; | ||
|
|
||
| return !(hasNegative && hasPositive); | ||
| } | ||
|
|
||
| /** | ||
| * Builds the "safe triangle" connecting `origin` (typically the pointer position where it left a | ||
| * trigger) to the top and bottom corners of `targetRect` (typically a submenu panel) that are nearest | ||
| * to `origin` — the submenu can open on either side of its trigger, so the nearest edge is picked by | ||
| * comparing distances rather than assuming a fixed side. | ||
| * @docs-private | ||
| */ | ||
| export function getSafeTriangleVertices(origin: KbqPoint, targetRect: DOMRect): KbqTriangle { | ||
| const nearX = | ||
| Math.abs(targetRect.left - origin.x) <= Math.abs(targetRect.right - origin.x) | ||
| ? targetRect.left | ||
| : targetRect.right; | ||
|
|
||
| return { a: origin, b: { x: nearX, y: targetRect.top }, c: { x: nearX, y: targetRect.bottom } }; | ||
| } |
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
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
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.
@rmnturov по умолчанию это поведение должно быть включено?