Skip to content

Commit e8c99e1

Browse files
committed
fix typing for modifiers option as single value
1 parent 3cbf7f4 commit e8c99e1

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

addon/-base-popper-modifier.ts

+17-19
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import {
1212
setPopperForElement,
1313
isModifier,
14+
type CustomPopperOptions,
1415
type PopperModifierDescription,
1516
} from './index';
1617
import {
@@ -23,9 +24,9 @@ export interface PopperSignature {
2324
Args: {
2425
Positional: [
2526
HTMLElement,
26-
...(Partial<PopperOptions> | PopperModifierDescription)[],
27+
...(Partial<CustomPopperOptions> | PopperModifierDescription)[],
2728
];
28-
Named: Partial<PopperOptions>;
29+
Named: Partial<CustomPopperOptions>;
2930
};
3031
Element: HTMLElement;
3132
}
@@ -39,8 +40,8 @@ function getPopperOptions(
3940

4041
// Positional args that are not modifiers should be treated as full "options" objects
4142
const allPositionalOptions = positionalArguments.filter<
42-
Partial<PopperOptions>
43-
>((arg): arg is Partial<PopperOptions> => !isModifier(arg));
43+
Partial<CustomPopperOptions>
44+
>((arg): arg is Partial<CustomPopperOptions> => !isModifier(arg));
4445

4546
// Positional args that are modifiers will extend the rest of the configuration
4647
const allPositionalModifiers =
@@ -49,28 +50,25 @@ function getPopperOptions(
4950
);
5051

5152
const { ...namedOptions } = named;
52-
const options: Partial<PopperOptions> = {
53+
const customOptions: Partial<CustomPopperOptions> = {
5354
...allPositionalOptions.reduce((acc, curr) => {
5455
return { ...acc, ...curr };
5556
}, {}),
5657
...namedOptions,
5758
};
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];
59+
const options: Partial<PopperOptions> = {
60+
...customOptions,
61+
// Ensure that the `modifiers` is always an array
62+
modifiers:
63+
customOptions.modifiers === undefined || isEmpty(customOptions.modifiers)
64+
? []
65+
: isArray(customOptions.modifiers)
66+
? customOptions.modifiers
67+
: [customOptions.modifiers],
68+
};
6669

6770
// Add runloop integration and positional modifiers to the array of modifiers
68-
options.modifiers = [
69-
...modifiers,
70-
...allPositionalModifiers,
71-
beginRunLoop,
72-
endRunLoop,
73-
];
71+
options.modifiers?.push(...allPositionalModifiers, beginRunLoop, endRunLoop);
7472

7573
return options;
7674
}

addon/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import {
22
type Instance as PopperInstance,
33
type Modifier as PopperModifier,
4+
type Options as PopperOptions,
45
} from '@popperjs/core';
56

67
const ELEMENT_TO_POPPER: WeakMap<Element, PopperInstance> = new WeakMap();
78

89
const IS_POPPER_MODIFIER = Symbol('is-popper-modifier');
910

11+
export interface CustomPopperOptions extends Omit<PopperOptions, 'modifiers'> {
12+
modifiers:
13+
| PopperOptions['modifiers']
14+
| Partial<PopperModifier<unknown, { [key: string]: unknown }>>;
15+
}
16+
1017
export type PopperModifierDescription = Partial<
1118
PopperModifier<unknown, { [key: string]: unknown }>
1219
> & {
@@ -37,7 +44,7 @@ export function setPopperForElement(
3744
* @return {object}
3845
*/
3946
export function createModifier(
40-
configuration: Partial<PopperModifier<unknown, { [key: string]: unknown }>>,
47+
configuration: CustomPopperOptions['modifiers'],
4148
): PopperModifierDescription {
4249
return {
4350
[IS_POPPER_MODIFIER]: true,

0 commit comments

Comments
 (0)