noph-ui 0.4.1 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,14 @@
2
2
  import { generateUUIDv4 } from '../utils.js'
3
3
  import type { MenuProps } from './types.ts'
4
4
 
5
- let { anchor, children, ...attributes }: MenuProps = $props()
5
+ let {
6
+ anchor,
7
+ children,
8
+ element = $bindable(),
9
+ showPopover = $bindable(),
10
+ hidePopover = $bindable(),
11
+ ...attributes
12
+ }: MenuProps = $props()
6
13
 
7
14
  let clientWidth = $state(0)
8
15
  let clientHeight = $state(0)
@@ -10,30 +17,38 @@
10
17
  let innerWidth = $state(0)
11
18
  let scrollY = $state(0)
12
19
  let scrollX = $state(0)
13
- let popoverElement: HTMLDivElement | undefined = $state()
14
20
  let anchorId = anchor?.style.getPropertyValue('anchor-name')
21
+
22
+ showPopover = () => {
23
+ element?.showPopover()
24
+ }
25
+
26
+ hidePopover = () => {
27
+ element?.hidePopover()
28
+ }
29
+
15
30
  const refreshValues = () => {
16
- if (popoverElement && anchor && !('anchorName' in document.documentElement.style)) {
31
+ if (element && anchor && !('anchorName' in document.documentElement.style)) {
17
32
  const anchorRect = anchor.getBoundingClientRect()
18
33
  if (anchorRect.bottom + clientHeight > innerHeight && anchorRect.top - clientHeight > 0) {
19
- popoverElement.style.top = `${anchorRect.top - clientHeight - 2}px`
34
+ element.style.top = `${anchorRect.top - clientHeight - 2}px`
20
35
  } else {
21
- popoverElement.style.top = `${anchorRect.bottom + 2}px`
36
+ element.style.top = `${anchorRect.bottom + 2}px`
22
37
  }
23
38
  const left = anchorRect.left + anchorRect.width / 2 - clientWidth / 2
24
39
  if (left > innerWidth - clientWidth) {
25
- popoverElement.style.left = `${innerWidth - clientWidth - 8}px`
40
+ element.style.left = `${innerWidth - clientWidth - 8}px`
26
41
  } else if (left < 8) {
27
- popoverElement.style.left = '8px'
42
+ element.style.left = '8px'
28
43
  } else {
29
- popoverElement.style.left = `${anchorRect.left + anchorRect.width / 2 - clientWidth / 2}px`
44
+ element.style.left = `${anchorRect.left + anchorRect.width / 2 - clientWidth / 2}px`
30
45
  }
31
46
  }
32
47
  }
33
48
  $effect(refreshValues)
34
49
 
35
50
  $effect(() => {
36
- if (anchor && popoverElement) {
51
+ if (anchor && element) {
37
52
  if (!('anchorName' in document.documentElement.style)) {
38
53
  anchor.addEventListener('click', () => {
39
54
  refreshValues()
@@ -47,7 +62,7 @@
47
62
  )
48
63
  } else if (!anchorId) {
49
64
  const generatedId = `--${generateUUIDv4()}`
50
- popoverElement.style.setProperty('position-anchor', generatedId)
65
+ element.style.setProperty('position-anchor', generatedId)
51
66
  anchor.style.setProperty('anchor-name', generatedId)
52
67
  }
53
68
  }
@@ -63,7 +78,7 @@
63
78
  />
64
79
 
65
80
  <div
66
- bind:this={popoverElement}
81
+ bind:this={element}
67
82
  bind:clientWidth
68
83
  bind:clientHeight
69
84
  {...attributes}
@@ -1,4 +1,4 @@
1
1
  import type { MenuProps } from './types.ts';
2
- declare const Menu: import("svelte").Component<MenuProps, {}, "">;
2
+ declare const Menu: import("svelte").Component<MenuProps, {}, "element" | "showPopover" | "hidePopover">;
3
3
  type Menu = ReturnType<typeof Menu>;
4
4
  export default Menu;
@@ -3,6 +3,9 @@ import type { HTMLAttributes, HTMLAnchorAttributes, HTMLButtonAttributes } from
3
3
  export interface MenuProps extends HTMLAttributes<HTMLDivElement> {
4
4
  children: Snippet;
5
5
  anchor?: HTMLElement | undefined;
6
+ showPopover?: () => void;
7
+ hidePopover?: () => void;
8
+ element?: HTMLDivElement;
6
9
  }
7
10
  interface ButtonProps extends HTMLButtonAttributes {
8
11
  selected?: boolean;
@@ -16,19 +16,19 @@
16
16
  noAsterisk = false,
17
17
  variant = 'filled',
18
18
  placeholder = ' ',
19
+ element = $bindable(),
19
20
  ...attributes
20
21
  }: TextFieldProps = $props()
21
22
 
22
- let contentEl: HTMLInputElement | HTMLTextAreaElement | undefined = $state()
23
23
  let errorTextRaw = $state(errorText)
24
24
 
25
25
  $effect(() => {
26
- if (contentEl) {
27
- contentEl.form?.addEventListener('reset', () => {
26
+ if (element) {
27
+ element.form?.addEventListener('reset', () => {
28
28
  error = false
29
29
  value = ''
30
30
  })
31
- contentEl.addEventListener('invalid', (event) => {
31
+ element.addEventListener('invalid', (event) => {
32
32
  event.preventDefault()
33
33
  const { currentTarget } = event as Event & {
34
34
  currentTarget: HTMLInputElement | HTMLTextAreaElement
@@ -42,7 +42,7 @@
42
42
  }
43
43
  })
44
44
 
45
- contentEl.addEventListener('change', (event) => {
45
+ element.addEventListener('change', (event) => {
46
46
  const { currentTarget } = event as Event & {
47
47
  currentTarget: HTMLInputElement | HTMLTextAreaElement
48
48
  }
@@ -68,7 +68,7 @@
68
68
  if (attributes.disabled) {
69
69
  return
70
70
  }
71
- contentEl?.focus()
71
+ element?.focus()
72
72
  }}
73
73
  >
74
74
  <div
@@ -117,7 +117,7 @@
117
117
  <textarea
118
118
  {...attributes}
119
119
  bind:value
120
- bind:this={contentEl}
120
+ bind:this={element}
121
121
  class="input"
122
122
  aria-label={label}
123
123
  {placeholder}
@@ -133,7 +133,7 @@
133
133
  <input
134
134
  {...attributes}
135
135
  bind:value
136
- bind:this={contentEl}
136
+ bind:this={element}
137
137
  class="input"
138
138
  {placeholder}
139
139
  aria-label={label}
@@ -1,4 +1,4 @@
1
1
  import type { TextFieldProps } from './types.ts';
2
- declare const TextField: import("svelte").Component<TextFieldProps, {}, "value">;
2
+ declare const TextField: import("svelte").Component<TextFieldProps, {}, "element" | "value">;
3
3
  type TextField = ReturnType<typeof TextField>;
4
4
  export default TextField;
@@ -12,6 +12,7 @@ export interface InputFieldProps extends Omit<HTMLInputAttributes, 'class'> {
12
12
  start?: Snippet;
13
13
  end?: Snippet;
14
14
  noAsterisk?: boolean;
15
+ element?: HTMLInputElement;
15
16
  }
16
17
  export interface TextAreaFieldProps extends Omit<HTMLTextareaAttributes, 'class'> {
17
18
  label?: string;
@@ -25,5 +26,6 @@ export interface TextAreaFieldProps extends Omit<HTMLTextareaAttributes, 'class'
25
26
  start?: Snippet;
26
27
  end?: Snippet;
27
28
  noAsterisk?: boolean;
29
+ element?: HTMLTextAreaElement;
28
30
  }
29
31
  export type TextFieldProps = InputFieldProps | TextAreaFieldProps;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "noph-ui",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "license": "MIT",
5
5
  "homepage": "https://noph.dev",
6
6
  "repository": {