looseleaf-uni 0.1.0 → 0.2.0

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.
@@ -1,159 +1,160 @@
1
- <script setup lang="ts">
2
- import { nextTick, onMounted, onUnmounted, ref, watch, type ComponentPublicInstance } from 'vue'
3
- import LlButton from '../ll-button/ll-button.vue'
4
- import LlPaper from '../ll-paper/ll-paper.vue'
5
- import { dialogProps, type DialogCloseReason } from './dialog'
6
-
7
- defineOptions({ options: { virtualHost: true } })
8
-
9
- const props = defineProps(dialogProps)
10
- const emit = defineEmits<{ 'update:open': [open: boolean]; 'close': [reason: DialogCloseReason] }>()
11
- const panel = ref<HTMLElement | ComponentPublicInstance>()
12
- let returnFocus: HTMLElement | undefined
13
- let mounted = false
14
-
15
- function requestClose(reason: DialogCloseReason) {
16
- // 父级拥有 open;拒绝回写时仍保持弹层打开。
17
- emit('update:open', false)
18
- emit('close', reason)
19
- }
20
-
21
- function backdrop() {
22
- if (props.closeOnBackdrop) requestClose('backdrop')
23
- }
24
-
25
- // #ifdef H5
26
- function panelElement() {
27
- return panel.value instanceof HTMLElement ? panel.value : panel.value?.$el as HTMLElement | undefined
28
- }
29
-
30
- function keyboard(event: KeyboardEvent) {
31
- if (event.key === 'Escape') {
32
- event.preventDefault()
33
- event.stopPropagation()
34
- requestClose('escape')
35
- }
36
- if (event.key !== 'Tab') return
37
- const root = panelElement()
38
- const controls = root?.querySelectorAll<HTMLElement>('button:not(:disabled), input:not(:disabled), textarea:not(:disabled), select:not(:disabled), a[href], [tabindex="0"]')
39
- if (!controls?.length) {
40
- event.preventDefault()
41
- return
42
- }
43
- const first = controls[0]!
44
- const last = controls[controls.length - 1]!
45
- const origin = document.activeElement
46
- // 保持 Tab 循环在弹层内,焦点离开时从相应端点重新进入。
47
- if (event.shiftKey && (origin === first || origin === root) || !event.shiftKey && (origin === last || origin === root)) {
48
- event.preventDefault()
49
- if (event.shiftKey) last.focus()
50
- else first.focus()
51
- }
52
- }
53
-
54
- async function syncFocus(open: boolean) {
55
- if (open) {
56
- if (document.activeElement instanceof HTMLElement) returnFocus = document.activeElement
57
- await nextTick()
58
- const root = panelElement()
59
- if (mounted && props.open && root && !root.contains(document.activeElement)) root.focus()
60
- } else {
61
- await nextTick()
62
- if (mounted && !props.open) returnFocus?.focus()
63
- }
64
- }
65
-
66
- onMounted(() => {
67
- mounted = true
68
- if (props.open) void syncFocus(true)
69
- })
70
- watch(panel, (_, __, onCleanup) => {
71
- const element = panelElement()
72
- if (!element) return
73
- // 只在 H5 挂到真实面板上,面板关闭或销毁时同步移除监听。
74
- element.addEventListener('keydown', keyboard)
75
- onCleanup(() => element.removeEventListener('keydown', keyboard))
76
- }, { flush: 'post' })
77
- watch(() => props.open, syncFocus)
78
- onUnmounted(() => {
79
- mounted = false
80
- returnFocus?.focus()
81
- })
82
- // #endif
83
- </script>
84
-
85
- <template>
86
- <view
87
- v-if="open"
88
- class="ll-dialog"
89
- :class="'ll-dialog--' + placement"
90
- >
91
- <view
92
- class="ll-dialog__backdrop"
93
- aria-hidden="true"
94
- @click.stop="backdrop"
95
- @touchmove.stop.prevent
96
- />
97
- <view
98
- ref="panel"
99
- class="ll-dialog__panel"
100
- role="dialog"
101
- aria-modal="true"
102
- :aria-label="title"
103
- tabindex="-1"
104
- >
105
- <ll-paper :tilt="false">
106
- <view class="ll-dialog__inside">
107
- <view class="ll-dialog__header">
108
- <text class="ll-dialog__title">
109
- {{ title }}
110
- </text>
111
- <ll-button
112
- v-if="showCloseButton"
113
- variant="ghost"
114
- size="icon-sm"
115
- aria-label="关闭"
116
- @click="requestClose('close-button')"
117
- >
118
- ×
119
- </ll-button>
120
- </view>
121
- <view class="ll-dialog__body">
122
- <slot />
123
- </view>
124
- <view
125
- v-if="$slots.footer"
126
- class="ll-dialog__footer"
127
- >
128
- <slot name="footer" />
129
- </view>
130
- </view>
131
- </ll-paper>
132
- </view>
133
- </view>
134
- </template>
135
-
136
- <style scoped>
137
- /* 在页面根挂载时,背景、滚动内容和安全区各占独立层。 */
138
- .ll-dialog { position: fixed; z-index: 1000; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; }
139
- .ll-dialog__backdrop { position: absolute; inset: 0; background: #261e1765; pointer-events: auto; }
140
- .ll-dialog__panel { position: relative; box-sizing: border-box; max-height: 88vh; width: 440px; max-width: calc(100% - 32px); overflow: auto; overscroll-behavior: contain; outline: none; pointer-events: auto; }
141
- .ll-dialog__inside { padding: 20px 24px 24px; }
142
- .ll-dialog__header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
143
- .ll-dialog__title { font-size: 21px; font-weight: 700; line-height: 1.3; }
144
- .ll-dialog__body { margin-top: 16px; }
145
- .ll-dialog__footer { margin-top: 20px; }
146
- @media (max-width: 639px) {
147
- .ll-dialog--center { align-items: flex-end; }
148
- .ll-dialog--center .ll-dialog__panel { max-width: 100%; width: 100%; }
149
- .ll-dialog__inside { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
150
- }
151
- .ll-dialog--top { align-items: flex-start; }
152
- .ll-dialog--bottom { align-items: flex-end; }
153
- .ll-dialog--top .ll-dialog__panel, .ll-dialog--bottom .ll-dialog__panel { width: 100%; max-width: 100%; }
154
- .ll-dialog--left, .ll-dialog--right { align-items: stretch; }
155
- .ll-dialog--left { justify-content: flex-start; }
156
- .ll-dialog--right { justify-content: flex-end; }
157
- .ll-dialog--left .ll-dialog__panel, .ll-dialog--right .ll-dialog__panel { max-height: 100%; max-width: 92%; }
158
- .ll-dialog--left .ll-dialog__inside, .ll-dialog--right .ll-dialog__inside { min-height: 100vh; box-sizing: border-box; }
159
- </style>
1
+ <script setup lang="ts">
2
+ import { nextTick, onMounted, onUnmounted, ref, watch, type ComponentPublicInstance } from 'vue'
3
+ import LlButton from '../ll-button/ll-button.vue'
4
+ import LlPaper from '../ll-paper/ll-paper.vue'
5
+ import { dialogProps, type DialogCloseReason } from './dialog'
6
+
7
+ defineOptions({ options: { virtualHost: true } })
8
+
9
+ const props = defineProps(dialogProps)
10
+ const emit = defineEmits<{ 'update:open': [open: boolean]; 'close': [reason: DialogCloseReason] }>()
11
+ const panel = ref<HTMLElement | ComponentPublicInstance>()
12
+ let returnFocus: HTMLElement | undefined
13
+ let mounted = false
14
+
15
+ function requestClose(reason: DialogCloseReason) {
16
+ // 父级拥有 open;拒绝回写时仍保持弹层打开。
17
+ emit('update:open', false)
18
+ emit('close', reason)
19
+ }
20
+
21
+ function backdrop() {
22
+ if (props.closeOnBackdrop) requestClose('backdrop')
23
+ }
24
+
25
+ // #ifdef H5
26
+ function panelElement() {
27
+ return panel.value instanceof HTMLElement ? panel.value : panel.value?.$el as HTMLElement | undefined
28
+ }
29
+
30
+ function keyboard(event: KeyboardEvent) {
31
+ if (event.key === 'Escape') {
32
+ event.preventDefault()
33
+ event.stopPropagation()
34
+ requestClose('escape')
35
+ }
36
+ if (event.key !== 'Tab') return
37
+ const root = panelElement()
38
+ const controls = root?.querySelectorAll<HTMLElement>('button:not(:disabled), input:not(:disabled), textarea:not(:disabled), select:not(:disabled), a[href], [tabindex="0"]')
39
+ if (!controls?.length) {
40
+ event.preventDefault()
41
+ return
42
+ }
43
+ const first = controls[0]!
44
+ const last = controls[controls.length - 1]!
45
+ const origin = document.activeElement
46
+ // 保持 Tab 循环在弹层内,焦点离开时从相应端点重新进入。
47
+ if (event.shiftKey && (origin === first || origin === root) || !event.shiftKey && (origin === last || origin === root)) {
48
+ event.preventDefault()
49
+ if (event.shiftKey) last.focus()
50
+ else first.focus()
51
+ }
52
+ }
53
+
54
+ async function syncFocus(open: boolean) {
55
+ if (open) {
56
+ if (document.activeElement instanceof HTMLElement) returnFocus = document.activeElement
57
+ await nextTick()
58
+ const root = panelElement()
59
+ if (mounted && props.open && root && !root.contains(document.activeElement)) root.focus()
60
+ } else {
61
+ await nextTick()
62
+ if (mounted && !props.open) returnFocus?.focus()
63
+ }
64
+ }
65
+
66
+ onMounted(() => {
67
+ mounted = true
68
+ if (props.open) void syncFocus(true)
69
+ })
70
+ watch(panel, (_, __, onCleanup) => {
71
+ const element = panelElement()
72
+ if (!element) return
73
+ // 只在 H5 挂到真实面板上,面板关闭或销毁时同步移除监听。
74
+ element.addEventListener('keydown', keyboard)
75
+ onCleanup(() => element.removeEventListener('keydown', keyboard))
76
+ }, { flush: 'post' })
77
+ watch(() => props.open, syncFocus)
78
+ onUnmounted(() => {
79
+ mounted = false
80
+ returnFocus?.focus()
81
+ })
82
+ // #endif
83
+ </script>
84
+
85
+ <template>
86
+ <view
87
+ v-if="open"
88
+ class="ll-dialog"
89
+ :class="'ll-dialog--' + placement"
90
+ >
91
+ <view
92
+ class="ll-dialog__backdrop"
93
+ aria-hidden="true"
94
+ @click.stop="backdrop"
95
+ @touchmove.stop.prevent
96
+ />
97
+ <view
98
+ ref="panel"
99
+ class="ll-dialog__panel"
100
+ role="dialog"
101
+ aria-modal="true"
102
+ :aria-label="title"
103
+ tabindex="-1"
104
+ >
105
+ <ll-paper :tilt="false">
106
+ <view class="ll-dialog__inside">
107
+ <view class="ll-dialog__header">
108
+ <text class="ll-dialog__title">
109
+ {{ title }}
110
+ </text>
111
+ <ll-button
112
+ v-if="showCloseButton"
113
+ variant="ghost"
114
+ size="icon-sm"
115
+ aria-label="关闭"
116
+ @click="requestClose('close-button')"
117
+ >
118
+ ×
119
+ </ll-button>
120
+ </view>
121
+ <view class="ll-dialog__body">
122
+ <slot />
123
+ </view>
124
+ <view
125
+ v-if="$slots.footer"
126
+ class="ll-dialog__footer"
127
+ >
128
+ <slot name="footer" />
129
+ </view>
130
+ </view>
131
+ </ll-paper>
132
+ </view>
133
+ </view>
134
+ </template>
135
+
136
+ <style scoped>
137
+ /* 在页面根挂载时,背景、滚动内容和安全区各占独立层。 */
138
+ .ll-dialog { position: fixed; z-index: 1000; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; }
139
+ .ll-dialog__backdrop { position: absolute; inset: 0; background: #261e1765; pointer-events: auto; }
140
+ .ll-dialog__panel { position: relative; box-sizing: border-box; max-height: 88vh; width: 440px; max-width: calc(100% - 32px); overflow: auto; overscroll-behavior: contain; outline: none; pointer-events: auto; }
141
+ .ll-dialog__inside { padding: 20px 24px 24px; }
142
+ .ll-dialog__header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
143
+ /* 宿主可单独调整标题字体,正文和操作区继续按各自的排版渲染。 */
144
+ .ll-dialog__title { font-family: var(--ll-dialog-title-font-family, inherit); font-size: 21px; font-weight: var(--ll-dialog-title-font-weight, 700); line-height: 1.3; }
145
+ .ll-dialog__body { margin-top: 16px; }
146
+ .ll-dialog__footer { margin-top: 20px; }
147
+ @media (max-width: 639px) {
148
+ .ll-dialog--center { align-items: flex-end; }
149
+ .ll-dialog--center .ll-dialog__panel { max-width: 100%; width: 100%; }
150
+ .ll-dialog__inside { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
151
+ }
152
+ .ll-dialog--top { align-items: flex-start; }
153
+ .ll-dialog--bottom { align-items: flex-end; }
154
+ .ll-dialog--top .ll-dialog__panel, .ll-dialog--bottom .ll-dialog__panel { width: 100%; max-width: 100%; }
155
+ .ll-dialog--left, .ll-dialog--right { align-items: stretch; }
156
+ .ll-dialog--left { justify-content: flex-start; }
157
+ .ll-dialog--right { justify-content: flex-end; }
158
+ .ll-dialog--left .ll-dialog__panel, .ll-dialog--right .ll-dialog__panel { max-height: 100%; max-width: 92%; }
159
+ .ll-dialog--left .ll-dialog__inside, .ll-dialog--right .ll-dialog__inside { min-height: 100vh; box-sizing: border-box; }
160
+ </style>
@@ -1,37 +1,41 @@
1
- <script setup lang="ts">
2
- import { computed } from 'vue'
3
- import { renderArt } from '../../common/art'
4
- import glyphs from '../../common/icons.json'
5
- import aliases from '../../common/icon-aliases.json'
6
- import { useLooseleafTheme } from '../../common/theme'
7
-
8
- const props = withDefaults(defineProps<{ name: string; size?: number; label?: string; flip?: boolean | 'x' | 'y' }>(), { size: 16 })
9
- const theme = useLooseleafTheme()
10
- const aliasMap = new Map(Object.entries(aliases).flatMap(([id, names]) => names.map(name => [name, id] as const)))
11
- const definition = computed(() => {
12
- const id = aliasMap.get(props.name) ?? props.name
13
- return Object.prototype.hasOwnProperty.call(glyphs, id) ? glyphs[id as keyof typeof glyphs] : undefined
14
- })
15
- const style = computed(() => ({
16
- width: props.size + 'px',
17
- height: props.size + 'px',
18
- backgroundImage: definition.value && renderArt(definition.value, props.size, theme.value.ink),
19
- transform: props.flip ? props.flip === 'y' ? 'scaleY(-1)' : 'scaleX(-1)' : 'none',
20
- }))
21
- </script>
22
-
23
- <template>
24
- <view
25
- v-if="definition"
26
- class="ll-icon"
27
- :style="style"
28
- :aria-hidden="!label"
29
- :role="label ? 'img' : undefined"
30
- :aria-label="label"
31
- />
32
- </template>
33
-
34
- <style scoped>
35
- /* 装饰图标默认略过,具名图标交给读屏报告。 */
36
- .ll-icon { display: inline-block; vertical-align: middle; flex-shrink: 0; background-repeat: no-repeat; background-size: contain; pointer-events: none; }
37
- </style>
1
+ <script setup lang="ts">
2
+ import { computed } from 'vue'
3
+ import { renderArt } from '../../common/art'
4
+ import glyphs from '../../common/icons.json'
5
+ import aliases from '../../common/icon-aliases.json'
6
+ import { useLooseleafTheme } from '../../common/theme'
7
+
8
+ const props = withDefaults(defineProps<{ name: string; size?: number; label?: string; flip?: boolean | 'x' | 'y' }>(), { size: 16 })
9
+ const theme = useLooseleafTheme()
10
+ // 避免微信开发者工具为数组解构引入无法加载的 SWC 运行时辅助模块。
11
+ const aliasMap = new Map<string, string>()
12
+ Object.entries(aliases).forEach(entry => {
13
+ entry[1].forEach(name => aliasMap.set(name, entry[0]))
14
+ })
15
+ const definition = computed(() => {
16
+ const id = aliasMap.get(props.name) ?? props.name
17
+ return Object.prototype.hasOwnProperty.call(glyphs, id) ? glyphs[id as keyof typeof glyphs] : undefined
18
+ })
19
+ const style = computed(() => ({
20
+ width: props.size + 'px',
21
+ height: props.size + 'px',
22
+ backgroundImage: definition.value && renderArt(definition.value, props.size, theme.value.ink),
23
+ transform: props.flip ? props.flip === 'y' ? 'scaleY(-1)' : 'scaleX(-1)' : 'none',
24
+ }))
25
+ </script>
26
+
27
+ <template>
28
+ <view
29
+ v-if="definition"
30
+ class="ll-icon"
31
+ :style="style"
32
+ :aria-hidden="!label"
33
+ :role="label ? 'img' : undefined"
34
+ :aria-label="label"
35
+ />
36
+ </template>
37
+
38
+ <style scoped>
39
+ /* 装饰图标默认略过,具名图标交给读屏报告。 */
40
+ .ll-icon { display: inline-block; vertical-align: middle; flex-shrink: 0; background-repeat: no-repeat; background-size: contain; pointer-events: none; }
41
+ </style>
@@ -1,73 +1,73 @@
1
- <script setup lang="ts">
2
- import { computed } from 'vue'
3
- import { inkOutline, svgBackground } from '../../common/ink'
4
- import { useLooseleafTheme } from '../../common/theme'
5
- import { useInkBox } from '../../composables/use-ink-box'
6
- import { inputProps } from './input'
7
-
8
- defineOptions({ options: { virtualHost: true } })
9
-
10
- const props = defineProps(inputProps)
11
- const emit = defineEmits<{
12
- 'update:modelValue': [value: string]
13
- 'focus': [event: unknown]
14
- 'blur': [event: unknown]
15
- 'confirm': [value: string]
16
- }>()
17
- const theme = useLooseleafTheme()
18
- const box = useInkBox('.ll-input')
19
- const style = computed(() => {
20
- const color = props.invalid ? theme.value.destructive : theme.value.muted
21
- const size = box.value
22
- return {
23
- color: theme.value.ink,
24
- backgroundColor: theme.value.slip,
25
- '--ll-input-stroke': color,
26
- '--ll-input-focus': theme.value.primary,
27
- backgroundImage: size && svgBackground(size.width, size.height, `<path d="${inkOutline(size.width, size.height, props.seed, 3)}" stroke="${color}" fill="none" stroke-width="1.4"/>`),
28
- }
29
- })
30
-
31
- function readValue(event: unknown) {
32
- return (event as { detail: { value: string } }).detail.value
33
- }
34
-
35
- function update(event: Event) {
36
- // 只使用 uni-app 定义的 detail.value,允许空串从输入框回写。
37
- emit('update:modelValue', readValue(event))
38
- }
39
- </script>
40
-
41
- <template>
42
- <view
43
- class="ll-input"
44
- :class="{ 'll-input--drawn': box }"
45
- :style="style"
46
- >
47
- <input
48
- :id="id"
49
- class="ll-input__control"
50
- :name="name"
51
- type="text"
52
- :value="modelValue"
53
- :placeholder="placeholder"
54
- :disabled="disabled"
55
- :focus="focus"
56
- :maxlength="maxlength"
57
- :aria-label="ariaLabel"
58
- :aria-invalid="invalid"
59
- @input="update"
60
- @focus="emit('focus', $event)"
61
- @blur="emit('blur', $event)"
62
- @confirm="emit('confirm', readValue($event))"
63
- >
64
- </view>
65
- </template>
66
-
67
- <style scoped>
68
- /* 输入框保留端侧输入法、键盘和确认键行为。 */
69
- .ll-input { position: relative; border: 1px solid var(--ll-input-stroke); border-radius: 3px; background-size: 100% 100%; }
70
- .ll-input--drawn { border-color: transparent; }
71
- .ll-input:focus-within { outline: 2px dashed var(--ll-input-focus); outline-offset: 2px; }
72
- .ll-input__control { box-sizing: border-box; width: 100%; height: 40px; padding: 0 12px; font-size: 16px; color: inherit; }
73
- </style>
1
+ <script setup lang="ts">
2
+ import { computed } from 'vue'
3
+ import { inkOutline, svgBackground } from '../../common/ink'
4
+ import { useLooseleafTheme } from '../../common/theme'
5
+ import { useInkBox } from '../../composables/use-ink-box'
6
+ import { inputProps } from './input'
7
+
8
+ defineOptions({ options: { virtualHost: true } })
9
+
10
+ const props = defineProps(inputProps)
11
+ const emit = defineEmits<{
12
+ 'update:modelValue': [value: string]
13
+ 'focus': [event: unknown]
14
+ 'blur': [event: unknown]
15
+ 'confirm': [value: string]
16
+ }>()
17
+ const theme = useLooseleafTheme()
18
+ const box = useInkBox('.ll-input')
19
+ const style = computed(() => {
20
+ const color = props.invalid ? theme.value.destructive : theme.value.muted
21
+ const size = box.value
22
+ return {
23
+ color: theme.value.ink,
24
+ backgroundColor: theme.value.slip,
25
+ '--ll-input-stroke': color,
26
+ '--ll-input-focus': theme.value.primary,
27
+ backgroundImage: size && svgBackground(size.width, size.height, `<path d="${inkOutline(size.width, size.height, props.seed, 3)}" stroke="${color}" fill="none" stroke-width="1.4"/>`),
28
+ }
29
+ })
30
+
31
+ function readValue(event: unknown) {
32
+ return (event as { detail: { value: string } }).detail.value
33
+ }
34
+
35
+ function update(event: unknown) {
36
+ // 微信事件不实现 DOM Event,只读取 uni-app 定义的 detail.value。
37
+ emit('update:modelValue', readValue(event))
38
+ }
39
+ </script>
40
+
41
+ <template>
42
+ <view
43
+ class="ll-input"
44
+ :class="{ 'll-input--drawn': box }"
45
+ :style="style"
46
+ >
47
+ <input
48
+ :id="id"
49
+ class="ll-input__control"
50
+ :name="name"
51
+ type="text"
52
+ :value="modelValue"
53
+ :placeholder="placeholder"
54
+ :disabled="disabled"
55
+ :focus="focus"
56
+ :maxlength="maxlength"
57
+ :aria-label="ariaLabel"
58
+ :aria-invalid="invalid"
59
+ @input="update"
60
+ @focus="emit('focus', $event)"
61
+ @blur="emit('blur', $event)"
62
+ @confirm="emit('confirm', readValue($event))"
63
+ >
64
+ </view>
65
+ </template>
66
+
67
+ <style scoped>
68
+ /* 输入框保留端侧输入法、键盘和确认键行为。 */
69
+ .ll-input { position: relative; border: 1px solid var(--ll-input-stroke); border-radius: 3px; background-size: 100% 100%; }
70
+ .ll-input--drawn { border-color: transparent; }
71
+ .ll-input:focus-within { outline: 2px dashed var(--ll-input-focus); outline-offset: 2px; }
72
+ .ll-input__control { box-sizing: border-box; width: 100%; height: 40px; padding: 0 12px; font-size: 16px; color: inherit; }
73
+ </style>