-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #289 from universal-ember/absorb-velcro
Injest all of ember-velcro as a new optional import: `ember-primitives/floating-ui`
- Loading branch information
Showing
16 changed files
with
711 additions
and
34 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
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as FloatingUI } from './floating-ui/component.gts'; | ||
export { default as floatingUI } from './floating-ui/modifier.ts'; |
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,80 @@ | ||
import Component from '@glimmer/component'; | ||
import { tracked } from '@glimmer/tracking'; | ||
import { hash } from '@ember/helper'; | ||
|
||
import { modifier } from 'ember-modifier'; | ||
|
||
import VelcroModifier from './modifier.ts'; | ||
|
||
import type { Signature as ModifierSignature } from './modifier.ts'; | ||
import type { MiddlewareState } from '@floating-ui/dom'; | ||
import type { WithBoundArgs, WithBoundPositionals } from '@glint/template'; | ||
import type { ModifierLike } from '@glint/template'; | ||
|
||
type ModifierArgs = ModifierSignature['Args']['Named']; | ||
|
||
export interface Signature { | ||
Args: { | ||
middleware?: ModifierArgs['middleware']; | ||
placement?: ModifierArgs['placement']; | ||
strategy?: ModifierArgs['strategy']; | ||
flipOptions?: ModifierArgs['flipOptions']; | ||
hideOptions?: ModifierArgs['hideOptions']; | ||
shiftOptions?: ModifierArgs['shiftOptions']; | ||
offsetOptions?: ModifierArgs['offsetOptions']; | ||
}; | ||
Blocks: { | ||
default: [ | ||
velcro: { | ||
hook: ModifierLike<HookSignature>; | ||
setHook: (element: HTMLElement | SVGElement) => void; | ||
loop?: WithBoundArgs<WithBoundPositionals<typeof VelcroModifier, 1>, keyof ModifierArgs>; | ||
data?: MiddlewareState; | ||
}, | ||
]; | ||
}; | ||
} | ||
|
||
interface HookSignature { | ||
Element: HTMLElement | SVGElement; | ||
} | ||
|
||
export default class Velcro extends Component<Signature> { | ||
@tracked hook?: HTMLElement | SVGElement = undefined; | ||
|
||
// set by VelcroModifier | ||
@tracked velcroData?: MiddlewareState = undefined; | ||
|
||
setVelcroData: ModifierArgs['setVelcroData'] = (data) => (this.velcroData = data); | ||
|
||
setHook = (element: HTMLElement | SVGElement) => { | ||
this.hook = element; | ||
}; | ||
|
||
velcroHook = modifier<HookSignature>((element: HTMLElement | SVGElement) => { | ||
this.setHook(element); | ||
}); | ||
|
||
<template> | ||
{{#let | ||
(modifier | ||
VelcroModifier | ||
flipOptions=@flipOptions | ||
hideOptions=@hideOptions | ||
middleware=@middleware | ||
offsetOptions=@offsetOptions | ||
placement=@placement | ||
shiftOptions=@shiftOptions | ||
strategy=@strategy | ||
setVelcroData=this.setVelcroData | ||
) | ||
as |loop| | ||
}} | ||
{{#let (if this.hook (modifier loop this.hook)) as |loopWithHook|}} | ||
{{yield | ||
(hash hook=this.velcroHook setHook=this.setHook loop=loopWithHook data=this.velcroData) | ||
}} | ||
{{/let}} | ||
{{/let}} | ||
</template> | ||
} |
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,13 @@ | ||
import type { Middleware } from '@floating-ui/dom'; | ||
|
||
export function velcroData(): Middleware { | ||
return { | ||
name: 'metadata', | ||
fn: (data) => { | ||
// https://floating-ui.com/docs/middleware#always-return-an-object | ||
return { | ||
data, | ||
}; | ||
}, | ||
}; | ||
} |
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,111 @@ | ||
import { assert } from '@ember/debug'; | ||
import { registerDestructor } from '@ember/destroyable'; | ||
|
||
import { autoUpdate, computePosition, flip, hide, offset, shift } from '@floating-ui/dom'; | ||
import Modifier from 'ember-modifier'; | ||
|
||
import { velcroData } from './middleware.ts'; | ||
|
||
import type { | ||
FlipOptions, | ||
HideOptions, | ||
Middleware, | ||
OffsetOptions, | ||
Placement, | ||
ShiftOptions, | ||
Strategy, | ||
} from '@floating-ui/dom'; | ||
|
||
export interface Signature { | ||
Element: HTMLElement; | ||
Args: { | ||
Positional: [referenceElement: string | HTMLElement | SVGElement]; | ||
Named: { | ||
strategy?: Strategy; | ||
offsetOptions?: OffsetOptions; | ||
placement?: Placement; | ||
flipOptions?: FlipOptions; | ||
shiftOptions?: ShiftOptions; | ||
hideOptions?: HideOptions; | ||
middleware?: Middleware[]; | ||
setVelcroData?: Middleware['fn']; | ||
}; | ||
}; | ||
} | ||
|
||
export default class VelcroModifier extends Modifier<Signature> { | ||
modify( | ||
floatingElement: Signature['Element'], | ||
[_referenceElement]: Signature['Args']['Positional'], | ||
{ | ||
strategy = 'fixed', | ||
offsetOptions = 0, | ||
placement = 'bottom', | ||
flipOptions, | ||
shiftOptions, | ||
middleware = [], | ||
setVelcroData, | ||
}: Signature['Args']['Named'] | ||
) { | ||
const referenceElement: null | HTMLElement | SVGElement = | ||
typeof _referenceElement === 'string' | ||
? document.querySelector(_referenceElement) | ||
: _referenceElement; | ||
|
||
assert( | ||
'no reference element defined', | ||
referenceElement instanceof HTMLElement || referenceElement instanceof SVGElement | ||
); | ||
|
||
assert( | ||
'no floating element defined', | ||
floatingElement instanceof HTMLElement || _referenceElement instanceof SVGElement | ||
); | ||
|
||
assert( | ||
'reference and floating elements cannot be the same element', | ||
floatingElement !== _referenceElement | ||
); | ||
|
||
assert('@middleware must be an array of one or more objects', Array.isArray(middleware)); | ||
|
||
Object.assign(floatingElement.style, { | ||
position: strategy, | ||
top: '0', | ||
left: '0', | ||
}); | ||
|
||
let update = async () => { | ||
let { middlewareData, x, y } = await computePosition(referenceElement, floatingElement, { | ||
middleware: [ | ||
offset(offsetOptions), | ||
flip(flipOptions), | ||
shift(shiftOptions), | ||
...middleware, | ||
hide({ strategy: 'referenceHidden' }), | ||
hide({ strategy: 'escaped' }), | ||
velcroData(), | ||
], | ||
placement, | ||
strategy, | ||
}); | ||
|
||
let referenceHidden = middlewareData.hide?.referenceHidden; | ||
|
||
Object.assign(floatingElement.style, { | ||
top: `${y}px`, | ||
left: `${x}px`, | ||
margin: 0, | ||
visibility: referenceHidden ? 'hidden' : 'visible', | ||
}); | ||
|
||
setVelcroData?.(middlewareData['metadata']); | ||
}; | ||
|
||
update(); | ||
|
||
let cleanup = autoUpdate(referenceElement, floatingElement, update); | ||
|
||
registerDestructor(this, cleanup); | ||
} | ||
} |
Oops, something went wrong.