-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dropdowns): added support for floating-ui (#2381)
- Loading branch information
Showing
17 changed files
with
161 additions
and
29 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 @@ | ||
--- | ||
"@ebay/ebayui-core": major | ||
--- | ||
|
||
feat(dropdowns): added support for floating-ui |
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,54 @@ | ||
import { | ||
autoUpdate, | ||
flip, | ||
computePosition, | ||
shift, | ||
offset, | ||
type ReferenceElement | ||
} from "@floating-ui/dom"; | ||
|
||
interface DropdownUtilOptions { | ||
reverse?: boolean; | ||
offset?: number | ||
} | ||
|
||
export class DropdownUtil { | ||
declare host: ReferenceElement; | ||
declare overlay: HTMLElement; | ||
declare cleanupFn: any; | ||
declare options: DropdownUtilOptions; | ||
|
||
constructor(host: HTMLElement, overlay: HTMLElement, options?: DropdownUtilOptions) { | ||
this.host = host as ReferenceElement; | ||
this.overlay = overlay as HTMLElement; | ||
this.options = options ?? {}; | ||
} | ||
|
||
show() { | ||
this.cleanupFn = autoUpdate( | ||
this.host, | ||
this.overlay, | ||
this.update.bind(this), | ||
); | ||
} | ||
|
||
update() { | ||
computePosition(this.host, this.overlay, { | ||
placement: this.options.reverse ? "bottom-end" : "bottom-start", | ||
middleware: [offset(this.options.offset ?? 4), flip(), shift()], | ||
}).then(({ x, y }) => { | ||
Object.assign(this.overlay.style, { | ||
left: `${x}px`, | ||
top: `${y}px`, | ||
}); | ||
}); | ||
} | ||
|
||
cleanup() { | ||
this.cleanupFn?.(); | ||
} | ||
|
||
hide() { | ||
if (this.cleanup) this.cleanup(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ $ const { | |
borderless, | ||
autocomplete, | ||
options, | ||
dropdownElement, | ||
floatingLabel, | ||
listSelection, | ||
expanded, | ||
|
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.