|
| 1 | +import Modifier, { type ArgsFor } from 'ember-modifier'; |
| 2 | +import { registerDestructor } from '@ember/destroyable'; |
| 3 | +import { isArray } from '@ember/array'; |
| 4 | +import { isEmpty } from '@ember/utils'; |
| 5 | +import { |
| 6 | + createPopper, |
| 7 | + type Instance as PopperInstance, |
| 8 | + type Options as PopperOptions, |
| 9 | +} from '@popperjs/core'; |
| 10 | + |
| 11 | +import { |
| 12 | + setPopperForElement, |
| 13 | + isModifier, |
| 14 | + type PopperModifierDescription, |
| 15 | +} from './index'; |
| 16 | +import { |
| 17 | + beginRunLoopModifier as beginRunLoop, |
| 18 | + endRunLoopModifier as endRunLoop, |
| 19 | +} from './in-runloop-modifier'; |
| 20 | +import type Owner from '@ember/owner'; |
| 21 | + |
| 22 | +export interface PopperSignature { |
| 23 | + Args: { |
| 24 | + Positional: [ |
| 25 | + HTMLElement, |
| 26 | + ...(Partial<PopperOptions> | PopperModifierDescription)[], |
| 27 | + ]; |
| 28 | + Named: Partial<PopperOptions>; |
| 29 | + }; |
| 30 | + Element: HTMLElement; |
| 31 | +} |
| 32 | + |
| 33 | +function getPopperOptions( |
| 34 | + positional: PopperSignature['Args']['Positional'], |
| 35 | + named: PopperSignature['Args']['Named'], |
| 36 | +): Partial<PopperOptions> { |
| 37 | + // Get an array of just positional "options"; first item is an element reference |
| 38 | + const [, ...positionalArguments] = positional; |
| 39 | + |
| 40 | + // Positional args that are not modifiers should be treated as full "options" objects |
| 41 | + const allPositionalOptions = positionalArguments.filter< |
| 42 | + Partial<PopperOptions> |
| 43 | + >((arg): arg is Partial<PopperOptions> => !isModifier(arg)); |
| 44 | + |
| 45 | + // Positional args that are modifiers will extend the rest of the configuration |
| 46 | + const allPositionalModifiers = |
| 47 | + positionalArguments.filter<PopperModifierDescription>( |
| 48 | + (arg): arg is PopperModifierDescription => isModifier(arg), |
| 49 | + ); |
| 50 | + |
| 51 | + const { ...namedOptions } = named; |
| 52 | + const options: Partial<PopperOptions> = { |
| 53 | + ...allPositionalOptions.reduce((acc, curr) => { |
| 54 | + return { ...acc, ...curr }; |
| 55 | + }, {}), |
| 56 | + ...namedOptions, |
| 57 | + }; |
| 58 | + |
| 59 | + // Ensure that the `modifiers` is always an array |
| 60 | + const modifiers = |
| 61 | + options.modifiers === undefined || isEmpty(options.modifiers) |
| 62 | + ? [] |
| 63 | + : isArray(options.modifiers) |
| 64 | + ? options.modifiers |
| 65 | + : [options.modifiers]; |
| 66 | + |
| 67 | + // Add runloop integration and positional modifiers to the array of modifiers |
| 68 | + options.modifiers = [ |
| 69 | + ...modifiers, |
| 70 | + ...allPositionalModifiers, |
| 71 | + beginRunLoop, |
| 72 | + endRunLoop, |
| 73 | + ]; |
| 74 | + |
| 75 | + return options; |
| 76 | +} |
| 77 | + |
| 78 | +export default abstract class PopperModifier extends Modifier<PopperSignature> { |
| 79 | + popper: PopperInstance | null = null; |
| 80 | + primaryElement: HTMLElement | null = null; |
| 81 | + secondaryElement: HTMLElement | null = null; |
| 82 | + |
| 83 | + abstract get tooltipElement(): HTMLElement; |
| 84 | + abstract get referenceElement(): HTMLElement; |
| 85 | + |
| 86 | + modify( |
| 87 | + element: PopperSignature['Element'], |
| 88 | + positionalArgs: PopperSignature['Args']['Positional'], |
| 89 | + namedArgs: PopperSignature['Args']['Named'], |
| 90 | + ): void { |
| 91 | + this.primaryElement = element; |
| 92 | + this.secondaryElement = positionalArgs[0]; |
| 93 | + |
| 94 | + const popperOptions = getPopperOptions(positionalArgs, namedArgs); |
| 95 | + |
| 96 | + // Create the popper once all required arguments are present |
| 97 | + if (!this.popper && this.referenceElement && this.tooltipElement) { |
| 98 | + this.popper = createPopper( |
| 99 | + this.referenceElement, |
| 100 | + this.tooltipElement, |
| 101 | + popperOptions, |
| 102 | + ); |
| 103 | + |
| 104 | + setPopperForElement(this.primaryElement, this.popper); |
| 105 | + } |
| 106 | + this.popper?.setOptions(popperOptions); |
| 107 | + } |
| 108 | + |
| 109 | + constructor(owner: Owner, args: ArgsFor<PopperSignature>) { |
| 110 | + super(owner, args); |
| 111 | + registerDestructor(this, this.cleanup); |
| 112 | + } |
| 113 | + |
| 114 | + cleanup = () => { |
| 115 | + this.popper?.destroy(); |
| 116 | + }; |
| 117 | +} |
0 commit comments