Skip to content

Commit

Permalink
.compute() and types Float & FloatingUI
Browse files Browse the repository at this point in the history
  • Loading branch information
Refzlund committed Jan 23, 2025
1 parent d6be0c5 commit b360d8b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
3 changes: 2 additions & 1 deletion floating-runes/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ for (let [key, path] of Object.entries(json.exports) as [string, string][]) {
path = path.replace(/^\.\/src/, './dist')
json.exports[key] = {
types: path.replace(/\.ts$/, '.d.ts'),
svelte: path.replace(/\.ts$/, '.js')
svelte: path.replace(/\.ts$/, '.js'),
default: path.replace(/\.ts$/, '.js')
}
}

Expand Down
17 changes: 15 additions & 2 deletions floating-runes/src/floating-ui.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ interface FloatOptions {
* {/if}
* ```
*/
export function floatingUI(options: FloatingRuneOptions = {}) {
function floatingUI(options: FloatingRuneOptions = {}) {
/** <float, arrow> */
const arrowMap = new WeakMap<HTMLElement, HTMLElement>()
const floatMap = new SvelteMap<HTMLElement, FloatOptions>()
Expand Down Expand Up @@ -242,6 +242,12 @@ export function floatingUI(options: FloatingRuneOptions = {}) {
if (arrowMap.has(node.parentElement!)) arrowMap.delete(node.parentElement!)
}
}
},
compute() {
if (!ref || floatMap.size === 0) return
for (const [float, floatOptions] of floatMap) {
compute(float, options, floatOptions)
}
}
}

Expand Down Expand Up @@ -348,4 +354,11 @@ export function floatingUI(options: FloatingRuneOptions = {}) {

type SvelteFloatingUI = typeof setFloat & typeof value
return setFloat as SvelteFloatingUI
}
}

export interface Float extends ReturnType<typeof floatingUI> {}
export interface FloatingUI {
(options?: FloatingRuneOptions): Float
}

export default floatingUI as FloatingUI
4 changes: 2 additions & 2 deletions floating-runes/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { floatingUI } from './floating-ui.svelte.js'

import { default as floatingUI } from './floating-ui.svelte.js'
export * from './floating-ui.svelte.js'

export default floatingUI

export * from './portal.svelte.js'
49 changes: 49 additions & 0 deletions floating-runes/src/wait.svelte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
let map = new WeakMap<Node, ReturnType<typeof wait>>()

export const wait = (ms: number) => {
let anchor: Node | undefined
try {
anchor = eval('node')
} catch (error) {
console.error(error)
}

console.log(anchor)

if(anchor) {
let wait = map.get(anchor)
if(wait) {
return wait
}
}

var awaited = $state(false)
let cancelled = $state(false)
let timeoutId: ReturnType<typeof setTimeout> | null = null

const wait = {
get awaited() {
return awaited
},
get cancelled() { return cancelled },
cancel: () => {
cancelled = true
if (timeoutId) {
clearTimeout(timeoutId)
}
},
promise: new Promise(res => {
timeoutId = setTimeout(() => {
if (cancelled) return
res(true)
console.log('true')
awaited = true
}, ms)
})
}

if(anchor) {
map.set(anchor, wait)
}
return wait
}

0 comments on commit b360d8b

Please sign in to comment.