Skip to content

Commit a7c340a

Browse files
authored
Merge pull request #2 from fedorovvvv/dynamic-autoUpdate
Dynamic auto update
2 parents c6c735c + f1219c7 commit a7c340a

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ Partial<[Options](https://floating-ui.com/docs/autoUpdate#options)>
110110
```ts
111111
/**
112112
* false: Don't initialize autoUpdate;
113-
* object: Initialization with its own parameters;
114-
* undefined: Standard autoUpdate values from the documentation;
115-
* @default undefined
113+
* true: Standard autoUpdate values from the documentation;
114+
* object: All as in the autoUpdate documentation. Your parameters are added to the default ones;
115+
* @default true
116116
*/
117-
autoUpdate?: false | Partial<Options>
117+
autoUpdate?: boolean | Partial<Options>
118118
```
119119

120120

src/lib/index.ts

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export type ComputeConfig = Omit<ComputePositionConfig, "platform"> & {
99
onComputed?: (computed: ComputePositionReturn) => void
1010
/**
1111
* false: Don't initialize autoUpdate;
12-
* object: Initialization with its own parameters;
13-
* undefined: Standard autoUpdate values from the documentation;
14-
* @default undefined
12+
* true: Standard autoUpdate values from the documentation;
13+
* object: All as in the autoUpdate documentation. Your parameters are added to the default ones;
14+
* @default true
1515
*/
16-
autoUpdate?: false | Partial<Options>
16+
autoUpdate?: boolean | Partial<Options>
1717
};
1818
export type UpdatePosition = (contentOptions?: Omit<ComputeConfig, 'autoUpdate'>) => void;
1919
export type ReferenceAction = (node: HTMLElement) => void;
@@ -23,11 +23,18 @@ export type ArrowOptions = { padding?: Padding, element: Writable<HTMLElement> }
2323
export function createFloatingActions(initOptions?: ComputeConfig): [ReferenceAction, ContentAction, UpdatePosition] {
2424
let referenceElement: ReferenceElement;
2525
let floatingElement: FloatingElement;
26+
const defaultOptions:Partial<ComputeConfig> = {
27+
autoUpdate: true
28+
}
2629
let options: ComputeConfig | undefined = initOptions;
2730

31+
const getOptions = (mixin?:object):ComputeConfig => {
32+
return {...defaultOptions, ...(initOptions || {}), ...(mixin || {}) }
33+
}
34+
2835
const updatePosition:UpdatePosition = (updateOptions) => {
2936
if (referenceElement && floatingElement) {
30-
options = { ...initOptions, ...updateOptions };
37+
options = getOptions(updateOptions);
3138
computePosition(referenceElement, floatingElement, options)
3239
.then(v => {
3340
Object.assign(floatingElement.style, {
@@ -48,15 +55,29 @@ export function createFloatingActions(initOptions?: ComputeConfig): [ReferenceAc
4855
const contentAction: ContentAction = (node, contentOptions) => {
4956
let autoUpdateDestroy:ReturnType<typeof _autoUpdate> | undefined
5057
floatingElement = node;
51-
options = { ...initOptions, ...contentOptions };
52-
updatePosition();
53-
if(options.autoUpdate !== false) {
54-
autoUpdateDestroy = _autoUpdate(referenceElement, floatingElement, () => updatePosition(), options.autoUpdate);
58+
options = getOptions(contentOptions);
59+
updatePosition(contentOptions);
60+
const destroyAutoUpdate = () => {
61+
if (autoUpdateDestroy) {
62+
autoUpdateDestroy()
63+
autoUpdateDestroy = undefined
64+
}
65+
}
66+
const initAutoUpdate = ({autoUpdate} = options || {}):typeof autoUpdateDestroy => {
67+
destroyAutoUpdate()
68+
if(autoUpdate !== false) {
69+
return _autoUpdate(referenceElement, floatingElement, () => updatePosition(options), (autoUpdate === true ? {} : autoUpdate));
70+
}
71+
return
5572
}
73+
autoUpdateDestroy = initAutoUpdate()
5674
return {
57-
update: updatePosition,
75+
update(contentOptions:Parameters<typeof contentAction>[1]) {
76+
updatePosition(contentOptions)
77+
autoUpdateDestroy = initAutoUpdate(contentOptions)
78+
},
5879
destroy() {
59-
autoUpdateDestroy && autoUpdateDestroy()
80+
destroyAutoUpdate()
6081
}
6182
}
6283
}

src/routes/index.svelte

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020
});
2121
},
2222
})
23-
]
23+
],
2424
});
25+
let autoUpdate = false
2526
</script>
2627

2728
<main>
29+
<button on:click="{() => autoUpdate = !autoUpdate}">
30+
toggle autoUpdate: {autoUpdate}
31+
</button>
2832
<div class="wrapper">
2933
<button
3034
on:click='{() => opened = !opened}'
@@ -33,7 +37,9 @@
3337
Show/Hide
3438
</button>
3539
{#if opened}
36-
<ul use:floatingContent>
40+
<ul use:floatingContent="{{
41+
autoUpdate
42+
}}">
3743
<li>OMG!</li>
3844
<li>NONONO!</li>
3945
</ul>
@@ -50,6 +56,7 @@
5056
}
5157
main {
5258
display: flex;
59+
flex-direction: column;
5360
align-items: center;
5461
justify-content: center;
5562
height: 500px;

0 commit comments

Comments
 (0)