@vrojs/base 0.0.1

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.
Files changed (46) hide show
  1. package/package.json +43 -0
  2. package/src/index.ts +8 -0
  3. package/src/style/base.scss +2 -0
  4. package/src/style/css.ts +2 -0
  5. package/src/style/index.scss +7 -0
  6. package/src/style/index.ts +1 -0
  7. package/src/types/Arrayable.ts +1 -0
  8. package/src/types/DeepPartial.ts +3 -0
  9. package/src/types/index.ts +2 -0
  10. package/src/utils/ComponentManager.ts +39 -0
  11. package/src/utils/format.ts +31 -0
  12. package/src/utils/index.ts +8 -0
  13. package/src/utils/mountComponent.ts +16 -0
  14. package/src/utils/parseType.ts +6 -0
  15. package/src/utils/props.ts +3 -0
  16. package/src/utils/renderComponent.ts +24 -0
  17. package/src/utils/showComponent.ts +24 -0
  18. package/src/utils/withInstall.ts +10 -0
  19. package/src/vro-aspect-ratio/README.md +55 -0
  20. package/src/vro-aspect-ratio/index.ts +15 -0
  21. package/src/vro-aspect-ratio/style/css.ts +2 -0
  22. package/src/vro-aspect-ratio/style/index.scss +16 -0
  23. package/src/vro-aspect-ratio/style/index.ts +1 -0
  24. package/src/vro-aspect-ratio/types.ts +15 -0
  25. package/src/vro-aspect-ratio/vro-aspect-ratio.vue +23 -0
  26. package/src/vro-input/README.md +95 -0
  27. package/src/vro-input/index.ts +15 -0
  28. package/src/vro-input/style/css.ts +2 -0
  29. package/src/vro-input/style/index.scss +0 -0
  30. package/src/vro-input/style/index.ts +1 -0
  31. package/src/vro-input/types.ts +112 -0
  32. package/src/vro-input/vro-input.vue +122 -0
  33. package/src/vro-loading/README.md +77 -0
  34. package/src/vro-loading/index.ts +15 -0
  35. package/src/vro-loading/style/css.ts +2 -0
  36. package/src/vro-loading/style/index.scss +119 -0
  37. package/src/vro-loading/style/index.ts +1 -0
  38. package/src/vro-loading/types.ts +39 -0
  39. package/src/vro-loading/vro-loading.vue +39 -0
  40. package/src/vro-vnr/README.md +46 -0
  41. package/src/vro-vnr/index.ts +15 -0
  42. package/src/vro-vnr/style/css.ts +2 -0
  43. package/src/vro-vnr/style/index.scss +0 -0
  44. package/src/vro-vnr/style/index.ts +1 -0
  45. package/src/vro-vnr/types.ts +10 -0
  46. package/src/vro-vnr/vro-vnr.vue +13 -0
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@vrojs/base",
3
+ "version": "0.0.1",
4
+ "description": "vrojs ui",
5
+ "main": "src/index.ts",
6
+ "types": "src/index.ts",
7
+ "files": [
8
+ "src",
9
+ "!src/**/demo",
10
+ "!src/**/README.md"
11
+ ],
12
+ "sideEffects": [
13
+ "src/**/style/*"
14
+ ],
15
+ "keywords": [
16
+ "vro",
17
+ "vrojs",
18
+ "ui",
19
+ "vue"
20
+ ],
21
+ "author": "woshiajuana",
22
+ "license": "MIT",
23
+ "publishConfig": {
24
+ "access": "public",
25
+ "registry": "https://registry.npmjs.org/"
26
+ },
27
+ "peerDependencies": {
28
+ "@daysnap/utils": ">=0.1.34",
29
+ "vue": ">=3.0.0",
30
+ "@vrojs/use": "0.0.1"
31
+ },
32
+ "devDependencies": {
33
+ "@daysnap/utils": "^0.1.34",
34
+ "rimraf": "^6.0.1",
35
+ "typescript": "^5.9.3",
36
+ "vue": "^3.5.22",
37
+ "@vrojs/use": "0.0.1"
38
+ },
39
+ "scripts": {
40
+ "release": "dsc publish --pnpm",
41
+ "sync": "dsc sync"
42
+ }
43
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ /* 本文件自动生成 './scripts/index.ts' */
2
+
3
+ export * from './types'
4
+ export * from './utils'
5
+ export * from './vro-aspect-ratio'
6
+ export * from './vro-input'
7
+ export * from './vro-loading'
8
+ export * from './vro-vnr'
@@ -0,0 +1,2 @@
1
+ :root {
2
+ }
@@ -0,0 +1,2 @@
1
+ import './base.scss'
2
+ import './index.scss'
@@ -0,0 +1,7 @@
1
+ /* 本文件自动生成 './scripts/index.ts' */
2
+
3
+ @use './base.scss' as *;
4
+ @use '../vro-aspect-ratio/style/index.scss' as *;
5
+ @use '../vro-input/style/index.scss' as *;
6
+ @use '../vro-loading/style/index.scss' as *;
7
+ @use '../vro-vnr/style/index.scss' as *;
@@ -0,0 +1 @@
1
+ import './css'
@@ -0,0 +1 @@
1
+ export type Arrayable<T> = T | T[]
@@ -0,0 +1,3 @@
1
+ export type DeepPartial<T> = {
2
+ [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]
3
+ }
@@ -0,0 +1,2 @@
1
+ export type * from './Arrayable'
2
+ export type * from './DeepPartial'
@@ -0,0 +1,39 @@
1
+ import { isFunction } from '@daysnap/utils'
2
+
3
+ type Constructor = abstract new (...args: any) => any
4
+
5
+ export type InstanceTypeProps<T extends Constructor> = InstanceType<T>['$props']
6
+
7
+ export type ComponentManagerMapValue<T extends Constructor = any> =
8
+ | {
9
+ is: T
10
+ props: InstanceTypeProps<T>
11
+ }
12
+ | { is: string; props: Record<string, any> }
13
+
14
+ export class ComponentManager {
15
+ map: Record<string, ComponentManagerMapValue> = {}
16
+
17
+ add(name: string, props?: Record<string, any>): ComponentManager
18
+ add<T extends Constructor>(name: string, is: T, props?: InstanceTypeProps<T>): ComponentManager
19
+ add<T extends Constructor>(name: string, is?: any, props?: InstanceTypeProps<T>) {
20
+ if (isFunction(is.setup)) {
21
+ this.map[name] = { is, props: props ?? {} }
22
+ } else {
23
+ this.map[name] = { is: name, props: is ?? {} }
24
+ }
25
+ return this
26
+ }
27
+
28
+ get(name: string) {
29
+ return this.map[name]
30
+ }
31
+
32
+ del(name: string) {
33
+ delete this.map[name]
34
+ }
35
+
36
+ clear() {
37
+ this.map = {}
38
+ }
39
+ }
@@ -0,0 +1,31 @@
1
+ export function parseNumberString(val: string) {
2
+ return val.replace(/[^\d]/g, '')
3
+ }
4
+
5
+ export function parseDecimalString(
6
+ val: string,
7
+ options: { precision?: number; allowNegativeNumber?: boolean } = {},
8
+ ) {
9
+ const { precision = 0, allowNegativeNumber = true } = options
10
+
11
+ // const reg = new RegExp(`^-?(0|[1-9]\\d*)?(\\.\\d{0,${precision}})?`, 'g')
12
+ const reg = new RegExp(
13
+ `^${allowNegativeNumber ? '-?' : ''}(0|[1-9]\\d*)?${+precision === 0 ? '' : `(\\.?\\d{0,${precision}})?`}`,
14
+ 'g',
15
+ )
16
+
17
+ val = val.match(reg)?.[0] ?? ''
18
+
19
+ // 特殊情况、特殊处理
20
+ if (val.startsWith('-00')) {
21
+ val = `-0`
22
+ } else if (val.startsWith('-.')) {
23
+ val = '-'
24
+ } else if (val.startsWith('.')) {
25
+ val = ''
26
+ } else if (val.startsWith('0') && !val.startsWith('0.') && val.length > 1) {
27
+ val = `${+val}`
28
+ }
29
+
30
+ return val
31
+ }
@@ -0,0 +1,8 @@
1
+ export * from './ComponentManager'
2
+ export * from './format'
3
+ export * from './mountComponent'
4
+ export * from './parseType'
5
+ export * from './props'
6
+ export * from './renderComponent'
7
+ export * from './showComponent'
8
+ export * from './withInstall'
@@ -0,0 +1,16 @@
1
+ import { type Component, createApp } from 'vue'
2
+
3
+ export function mountComponent(component: Component, props?: any) {
4
+ const root = document.createElement('div')
5
+ document.body.appendChild(root)
6
+
7
+ const instance = createApp(component, props)
8
+ instance.mount(root)
9
+
10
+ const destroy = () => {
11
+ instance.unmount()
12
+ document.body.removeChild(root)
13
+ }
14
+
15
+ return { instance, destroy }
16
+ }
@@ -0,0 +1,6 @@
1
+ export const parseType = (value: any) => {
2
+ if (Array.isArray(value)) {
3
+ return value.map((item) => item?.name).join(' , ')
4
+ }
5
+ return value?.name
6
+ }
@@ -0,0 +1,3 @@
1
+ import { PropType } from 'vue'
2
+
3
+ export const definePropType = <T>(val: any): PropType<T> => val
@@ -0,0 +1,24 @@
1
+ import { type AppContext, type Component, createVNode, render } from 'vue'
2
+
3
+ export function renderComponent(
4
+ component: Component,
5
+ props: any = {},
6
+ appContext: AppContext | null = null,
7
+ ) {
8
+ const container = document.createElement('div')
9
+ document.body.appendChild(container)
10
+
11
+ const vnode = createVNode(component, props)
12
+ vnode.appContext = appContext
13
+ render(vnode, container)
14
+
15
+ const destroy = () => {
16
+ render(null, container)
17
+ document.body.removeChild(container)
18
+ }
19
+
20
+ return {
21
+ instance: vnode.component!,
22
+ destroy,
23
+ }
24
+ }
@@ -0,0 +1,24 @@
1
+ import type { AppContext, Component } from 'vue'
2
+
3
+ import { renderComponent } from './renderComponent'
4
+
5
+ export function showComponent<
6
+ T extends Component,
7
+ P = T extends { new (): { $props: infer Props } } ? Props : Record<string, any>,
8
+ >(component: T, props: Partial<P>, appContext: AppContext | null = null) {
9
+ return new Promise<any>((resolve, reject) => {
10
+ const { destroy, instance } = renderComponent(
11
+ component,
12
+ {
13
+ ...props,
14
+ onClosed: () => destroy(),
15
+ },
16
+ appContext ??
17
+ (component as any)._context ??
18
+ (window as any).app?._context ??
19
+ (window as any)._app?._context,
20
+ )
21
+
22
+ ;(instance.exposed as any).show(props).then(resolve).catch(reject)
23
+ })
24
+ }
@@ -0,0 +1,10 @@
1
+ import type { App, Plugin } from 'vue'
2
+
3
+ export type WithInstall<T> = T & Plugin
4
+
5
+ export const withInstall = <T>(main: any): WithInstall<T> => {
6
+ ;(main as WithInstall<T>).install = (app: App): void => {
7
+ app.component(main.name, main)
8
+ }
9
+ return main
10
+ }
@@ -0,0 +1,55 @@
1
+ # VroAspectRatio
2
+
3
+ ### 介绍
4
+
5
+ 按照指定宽高比撑开容器,常用于图片、视频、卡片封面等固定比例区域。
6
+
7
+ ## 代码演示
8
+
9
+ ### 基础用法
10
+
11
+ ```html
12
+ <vro-aspect-ratio :ratio="16 / 9">
13
+ <div>16:9</div>
14
+ </vro-aspect-ratio>
15
+ ```
16
+
17
+ ## API
18
+
19
+ ### 属性 Props
20
+
21
+ <table>
22
+ <tbody>
23
+ <tr>
24
+ <td>名称</td>
25
+ <td>类型</td>
26
+ <td>默认值</td>
27
+ </tr>
28
+ <tr v-for="(item, key) in vroAspectRatioProps" :key="key">
29
+ <td>{{ key }}</td>
30
+ <td>{{ parseType(item.type || item) }}</td>
31
+ <td>{{ reserve(item.default, '-') }}</td>
32
+ </tr>
33
+ </tbody>
34
+ </table>
35
+
36
+ ### 插槽 Slots
37
+
38
+ <table>
39
+ <tbody>
40
+ <tr>
41
+ <td>名称</td>
42
+ <td>说明</td>
43
+ </tr>
44
+ <tr>
45
+ <td>default</td>
46
+ <td>内容</td>
47
+ </tr>
48
+ </tbody>
49
+ </table>
50
+
51
+ <script setup lang="ts">
52
+ import { reserve } from '@daysnap/utils'
53
+ import { VroAspectRatio, vroAspectRatioProps } from '.'
54
+ import { parseType } from '../utils'
55
+ </script>
@@ -0,0 +1,15 @@
1
+ import { withInstall } from '../utils'
2
+ import Component from './vro-aspect-ratio.vue'
3
+
4
+ export * from './types'
5
+
6
+ export const VroAspectRatio = withInstall<typeof Component>(Component)
7
+ export default VroAspectRatio
8
+
9
+ export type VroAspectRatioInstance = InstanceType<typeof VroAspectRatio>
10
+
11
+ declare module 'vue' {
12
+ export interface GlobalComponents {
13
+ VroAspectRatio: typeof VroAspectRatio
14
+ }
15
+ }
@@ -0,0 +1,2 @@
1
+ import '../../style/base.scss'
2
+ import './index.scss'
@@ -0,0 +1,16 @@
1
+ .vro-aspect-ratio {
2
+ position: relative;
3
+ }
4
+
5
+ .vro-aspect-ratio-inner {
6
+ position: relative;
7
+ width: 100%;
8
+ }
9
+
10
+ .vro-aspect-ratio-content {
11
+ position: absolute;
12
+ top: 0;
13
+ left: 0;
14
+ width: 100%;
15
+ height: 100%;
16
+ }
@@ -0,0 +1 @@
1
+ import './css'
@@ -0,0 +1,15 @@
1
+ import type { ExtractPropTypes } from 'vue'
2
+
3
+ export const vroAspectRatioProps = {
4
+ /**
5
+ * 宽高比,等价于 width / height
6
+ *
7
+ * @default 1
8
+ */
9
+ ratio: {
10
+ type: Number,
11
+ default: 1,
12
+ },
13
+ }
14
+
15
+ export type VroAspectRatioProps = ExtractPropTypes<typeof vroAspectRatioProps>
@@ -0,0 +1,23 @@
1
+ <template>
2
+ <div class="vro-aspect-ratio">
3
+ <div class="vro-aspect-ratio-inner" :style="innerStyle">
4
+ <div class="vro-aspect-ratio-content">
5
+ <slot></slot>
6
+ </div>
7
+ </div>
8
+ </div>
9
+ </template>
10
+
11
+ <script setup lang="ts">
12
+ import { computed } from 'vue'
13
+
14
+ import { vroAspectRatioProps } from './types'
15
+
16
+ defineOptions({ name: 'VroAspectRatio' })
17
+
18
+ const props = defineProps(vroAspectRatioProps)
19
+
20
+ const innerStyle = computed(() => ({
21
+ paddingTop: `${100 / props.ratio}%`,
22
+ }))
23
+ </script>
@@ -0,0 +1,95 @@
1
+ # VroInput
2
+
3
+ ### 介绍
4
+
5
+ 对原生 input 的轻量封装,只处理输入值的格式限制,不包含任何样式。ref、原生事件监听和其他原生属性由使用方按普通 input 方式处理。
6
+
7
+ ## 代码演示
8
+
9
+ ### 基础用法
10
+
11
+ ```html
12
+ <vro-input v-model="value" placeholder="请输入内容" />
13
+ ```
14
+
15
+ ### 数字输入
16
+
17
+ ```html
18
+ <vro-input v-model="value" type="number" />
19
+ ```
20
+
21
+ ### 小数输入
22
+
23
+ `type="decimal"` 会将输入限制为小数,并根据 `precision`、`min`、`max`、`auto-fix` 处理边界。
24
+
25
+ ```html
26
+ <vro-input v-model="value" type="decimal" :precision="2" :min="0" :max="9999" auto-fix />
27
+ ```
28
+
29
+ ### 自定义格式化
30
+
31
+ ```html
32
+ <vro-input v-model="value" :formatter="formatter" />
33
+ ```
34
+
35
+ ### 获取 input 元素
36
+
37
+ ```html
38
+ <template>
39
+ <vro-input ref="inputRef" v-model="value" />
40
+ </template>
41
+
42
+ <script setup lang="ts">
43
+ import { ref } from 'vue'
44
+ import type { VroInputInstance } from '@vrojs/base'
45
+
46
+ const value = ref('')
47
+ const inputRef = ref<VroInputInstance>()
48
+
49
+ inputRef.value?.input?.focus()
50
+ </script>
51
+ ```
52
+
53
+ ## API
54
+
55
+ ### 属性 Props
56
+
57
+ <table>
58
+ <tbody>
59
+ <tr>
60
+ <td>名称</td>
61
+ <td>类型</td>
62
+ <td>默认值</td>
63
+ </tr>
64
+ <tr v-for="(item, key) in vroInputProps" :key="key">
65
+ <td>{{ key }}</td>
66
+ <td>{{ parseType(item.type || item) }}</td>
67
+ <td>{{ reserve(item.default, '-') }}</td>
68
+ </tr>
69
+ </tbody>
70
+ </table>
71
+
72
+ ### 事件 Events
73
+
74
+ 其他同 input 原生事件
75
+
76
+ <table>
77
+ <tbody>
78
+ <tr>
79
+ <td>名称</td>
80
+ <td>参数</td>
81
+ <td>说明</td>
82
+ </tr>
83
+ <tr>
84
+ <td>update:modelValue</td>
85
+ <td>value: string</td>
86
+ <td>输入值处理后触发</td>
87
+ </tr>
88
+ </tbody>
89
+ </table>
90
+
91
+ <script setup lang="ts">
92
+ import { reserve } from '@daysnap/utils'
93
+ import { VroInput, vroInputProps } from '.'
94
+ import { parseType } from '../utils'
95
+ </script>
@@ -0,0 +1,15 @@
1
+ import { withInstall } from '../utils'
2
+ import Component from './vro-input.vue'
3
+
4
+ export * from './types'
5
+
6
+ export const VroInput = withInstall<typeof Component>(Component)
7
+ export default VroInput
8
+
9
+ export type VroInputInstance = InstanceType<typeof VroInput>
10
+
11
+ declare module 'vue' {
12
+ export interface GlobalComponents {
13
+ VroInput: typeof VroInput
14
+ }
15
+ }
@@ -0,0 +1,2 @@
1
+ import '../../style/base.scss'
2
+ import './index.scss'
File without changes
@@ -0,0 +1 @@
1
+ import './css'
@@ -0,0 +1,112 @@
1
+ import type { ExtractPropTypes, HTMLAttributes, InputTypeHTMLAttribute } from 'vue'
2
+
3
+ import { definePropType } from '../utils'
4
+
5
+ export const vroInputProps = {
6
+ /**
7
+ * 输入框类型。
8
+ *
9
+ * 在原生 input type 的基础上扩展了 decimal:
10
+ * - number:只允许输入数字,移动端使用数字键盘
11
+ * - decimal:只允许输入小数,移动端使用小数键盘
12
+ *
13
+ * @default text
14
+ */
15
+ type: {
16
+ type: definePropType<InputTypeHTMLAttribute | 'decimal'>(String),
17
+ default: 'text',
18
+ },
19
+
20
+ /**
21
+ * 输入框绑定值。
22
+ */
23
+ modelValue: {
24
+ type: definePropType<string | number | null | undefined>([String, Number]),
25
+ default: '',
26
+ },
27
+
28
+ /**
29
+ * 最大值,仅当 type 为 decimal 时作为输入值上限使用。
30
+ *
31
+ * @default Number.MAX_SAFE_INTEGER
32
+ */
33
+ max: {
34
+ type: [String, Number],
35
+ default: Number.MAX_SAFE_INTEGER,
36
+ },
37
+
38
+ /**
39
+ * 最小值,仅当 type 为 decimal 时作为输入值下限使用。
40
+ *
41
+ * @default Number.MIN_SAFE_INTEGER
42
+ */
43
+ min: {
44
+ type: [String, Number],
45
+ default: Number.MIN_SAFE_INTEGER,
46
+ },
47
+
48
+ /**
49
+ * 输入值超出 min/max 后是否自动修正到边界值,仅当 type 为 decimal 时有效。
50
+ *
51
+ * 为 false 时,超出边界会回退到上一次合法值。
52
+ *
53
+ * @default false
54
+ */
55
+ autoFix: Boolean,
56
+
57
+ /**
58
+ * 原生 maxlength 属性。
59
+ */
60
+ maxlength: [String, Number],
61
+
62
+ /**
63
+ * 原生 placeholder 属性。
64
+ */
65
+ placeholder: String,
66
+
67
+ /**
68
+ * 原生 disabled 属性。
69
+ */
70
+ disabled: Boolean,
71
+
72
+ /**
73
+ * 小数精度,仅当 type 为 decimal 时有效。
74
+ *
75
+ * @default 2
76
+ */
77
+ precision: {
78
+ type: [String, Number],
79
+ default: 2,
80
+ },
81
+
82
+ /**
83
+ * 输入值格式化函数,在内置 number/decimal 处理之后执行。
84
+ *
85
+ * value 为当前输入值,preValue 为上一次 modelValue。
86
+ */
87
+ formatter: definePropType<(value: string, preValue: string) => string>(Function),
88
+
89
+ /**
90
+ * 原生 id 属性。未传入时会自动生成一个 id。
91
+ */
92
+ id: String,
93
+
94
+ /**
95
+ * 原生 name 属性。
96
+ */
97
+ name: String,
98
+
99
+ /**
100
+ * 原生 readonly 属性。
101
+ */
102
+ readonly: Boolean,
103
+
104
+ /**
105
+ * 原生 inputmode 属性。
106
+ *
107
+ * 当 type 为 number 或 decimal 时,组件会自动设置更适合移动端的键盘类型。
108
+ */
109
+ inputmode: definePropType<HTMLAttributes['inputmode']>(String),
110
+ }
111
+
112
+ export type VroInputProps = ExtractPropTypes<typeof vroInputProps>
@@ -0,0 +1,122 @@
1
+ <template>
2
+ <input
3
+ v-bind="dynamicProps"
4
+ class="vro-input"
5
+ ref="inputRef"
6
+ :value="modelValue ?? ''"
7
+ @compositionstart="handleCompositionStart"
8
+ @compositionend="handleCompositionEnd"
9
+ @input="handleInput"
10
+ @blur="handleBlur"
11
+ />
12
+ </template>
13
+
14
+ <script setup lang="ts">
15
+ import { isNumber, omit } from '@daysnap/utils'
16
+ import { useComposition, useId } from '@vrojs/use'
17
+ import { computed, ref } from 'vue'
18
+
19
+ import { parseDecimalString, parseNumberString } from '../utils'
20
+ import { vroInputProps } from './types'
21
+
22
+ defineOptions({ name: 'VroInput' })
23
+ const props = defineProps(vroInputProps)
24
+ const emit = defineEmits<{
25
+ (event: 'update:modelValue', value: string): void
26
+ (event: 'blur', value: FocusEvent): void
27
+ (event: 'input', value: InputEvent): void
28
+ }>()
29
+
30
+ const inputRef = ref<HTMLInputElement>()
31
+ const fallbackId = useId()
32
+
33
+ const dynamicProps = computed(() => {
34
+ // eslint-disable-next-line prefer-const
35
+ let { type, inputmode, ...rest } = props
36
+
37
+ if (type === 'decimal') {
38
+ type = 'text'
39
+ inputmode = 'decimal'
40
+ } else if (type === 'number') {
41
+ type = 'tel'
42
+ inputmode = 'numeric'
43
+ }
44
+
45
+ return {
46
+ ...omit(rest, ['autoFix', 'formatter', 'modelValue', 'precision', 'max', 'min']),
47
+ id: props.id ?? fallbackId,
48
+ type,
49
+ inputmode,
50
+ }
51
+ })
52
+
53
+ const { handleCompositionStart, handleCompositionEnd } = useComposition()
54
+
55
+ const handleBlur = (event: FocusEvent) => {
56
+ const { type, modelValue } = props
57
+ if (type === 'decimal') {
58
+ const v = modelValue?.toString() ?? ''
59
+ if (v === '-') {
60
+ // 修复只输入 - 的情况
61
+ emit('update:modelValue', '')
62
+ } else if (v.endsWith('.')) {
63
+ // 修复末尾只输入.的情况
64
+ emit('update:modelValue', v.substring(0, v.length - 1))
65
+ } else if (v !== '' && +v === 0) {
66
+ // 修复 -0、-0.0 这种情况
67
+ emit('update:modelValue', '0')
68
+ }
69
+ }
70
+ emit('blur', event)
71
+ }
72
+
73
+ const handleInput = (e: InputEvent) => {
74
+ const target = e.target as HTMLInputElement
75
+ if ((target as any)?.composing) {
76
+ return
77
+ }
78
+
79
+ const { type, precision, min, max, formatter, modelValue, autoFix } = props
80
+ let value = target.value
81
+
82
+ if (type === 'number') {
83
+ value = parseNumberString(value)
84
+ } else if (type === 'decimal') {
85
+ value = parseDecimalString(value, { precision: +precision, allowNegativeNumber: +min < 0 })
86
+
87
+ const valueNumber = Number(value)
88
+ const minNumber = Number(min)
89
+ const maxNumber = Number(max)
90
+ const canCompare = value !== '' && value !== '-' && Number.isFinite(valueNumber)
91
+
92
+ if (canCompare && (valueNumber < minNumber || valueNumber > maxNumber)) {
93
+ if (autoFix) {
94
+ if (valueNumber < minNumber) {
95
+ value = min.toString()
96
+ } else if (valueNumber > maxNumber) {
97
+ value = max.toString()
98
+ }
99
+ } else {
100
+ value = modelValue?.toString() ?? ''
101
+ }
102
+ }
103
+ }
104
+
105
+ if (formatter) {
106
+ value = formatter(value, isNumber(modelValue) ? modelValue.toString() : (modelValue ?? ''))
107
+ }
108
+
109
+ if (target.value !== value) {
110
+ target.value = value
111
+ }
112
+
113
+ emit('update:modelValue', value)
114
+ emit('input', e)
115
+ }
116
+
117
+ defineExpose({
118
+ get input() {
119
+ return inputRef.value!
120
+ },
121
+ })
122
+ </script>
@@ -0,0 +1,77 @@
1
+ # VroLoading
2
+
3
+ ### 介绍
4
+
5
+ 纯 Vue/CSS 的加载图标,不依赖第三方库。支持圆环、线圈、点状三种形态,可配置尺寸和颜色。
6
+
7
+ ## 代码演示
8
+
9
+ ### 基础用法
10
+
11
+ ```html
12
+ <vro-loading />
13
+ ```
14
+
15
+ ### 加载类型
16
+
17
+ ```html
18
+ <vro-loading type="circular" />
19
+ <vro-loading type="spinner" />
20
+ <vro-loading type="dots" />
21
+ ```
22
+
23
+ ### 自定义样式
24
+
25
+ ```html
26
+ <vro-loading type="spinner" color="#409eff" :size="40" />
27
+ ```
28
+
29
+ ### 自定义图标
30
+
31
+ ```html
32
+ <vro-loading>
33
+ <template #icon>
34
+ <span class="custom-loading-icon"></span>
35
+ </template>
36
+ </vro-loading>
37
+ ```
38
+
39
+ ## API
40
+
41
+ ### 属性 Props
42
+
43
+ <table>
44
+ <tbody>
45
+ <tr>
46
+ <td>名称</td>
47
+ <td>类型</td>
48
+ <td>默认值</td>
49
+ </tr>
50
+ <tr v-for="(item, key) in vroLoadingProps" :key="key">
51
+ <td>{{ key }}</td>
52
+ <td>{{ parseType(item.type || item) }}</td>
53
+ <td>{{ reserve(item.default, '-') }}</td>
54
+ </tr>
55
+ </tbody>
56
+ </table>
57
+
58
+ ### 插槽 Slots
59
+
60
+ <table>
61
+ <tbody>
62
+ <tr>
63
+ <td>名称</td>
64
+ <td>说明</td>
65
+ </tr>
66
+ <tr>
67
+ <td>icon</td>
68
+ <td>自定义加载图标</td>
69
+ </tr>
70
+ </tbody>
71
+ </table>
72
+
73
+ <script setup lang="ts">
74
+ import { reserve } from '@daysnap/utils'
75
+ import { VroLoading, vroLoadingProps } from '.'
76
+ import { parseType } from '../utils'
77
+ </script>
@@ -0,0 +1,15 @@
1
+ import { withInstall } from '../utils'
2
+ import Component from './vro-loading.vue'
3
+
4
+ export * from './types'
5
+
6
+ export const VroLoading = withInstall<typeof Component>(Component)
7
+ export default VroLoading
8
+
9
+ export type VroLoadingInstance = InstanceType<typeof VroLoading>
10
+
11
+ declare module 'vue' {
12
+ export interface GlobalComponents {
13
+ VroLoading: typeof VroLoading
14
+ }
15
+ }
@@ -0,0 +1,2 @@
1
+ import '../../style/base.scss'
2
+ import './index.scss'
@@ -0,0 +1,119 @@
1
+ .vro-loading {
2
+ --vro-loading-size: 32px;
3
+ --vro-loading-color: currentColor;
4
+
5
+ display: inline-flex;
6
+ align-items: center;
7
+ justify-content: center;
8
+ color: var(--vro-loading-color);
9
+ line-height: 1;
10
+ vertical-align: middle;
11
+ }
12
+
13
+ .vro-loading__circular {
14
+ width: var(--vro-loading-size);
15
+ height: var(--vro-loading-size);
16
+ animation: vro-loading-rotate 1.6s linear infinite;
17
+
18
+ circle {
19
+ stroke: currentColor;
20
+ stroke-width: 4;
21
+ stroke-linecap: round;
22
+ stroke-dasharray: 90, 150;
23
+ stroke-dashoffset: 0;
24
+ animation: vro-loading-circular 1.6s ease-in-out infinite;
25
+ }
26
+ }
27
+
28
+ .vro-loading__spinner {
29
+ position: relative;
30
+ width: var(--vro-loading-size);
31
+ height: var(--vro-loading-size);
32
+ animation: vro-loading-fade 1.2s linear infinite;
33
+
34
+ i {
35
+ position: absolute;
36
+ left: 50%;
37
+ top: 0;
38
+ width: 2px;
39
+ height: 28%;
40
+ background: currentColor;
41
+ border-radius: 999px;
42
+ transform-origin: center calc(var(--vro-loading-size) / 2);
43
+
44
+ @for $i from 1 through 12 {
45
+ &:nth-child(#{$i}) {
46
+ opacity: calc(#{$i} / 12);
47
+ transform: translateX(-50%) rotate(#{($i - 1) * 30deg});
48
+ }
49
+ }
50
+ }
51
+ }
52
+
53
+ .vro-loading__dots {
54
+ display: inline-flex;
55
+ align-items: center;
56
+ justify-content: center;
57
+ gap: calc(var(--vro-loading-size) * 0.12);
58
+ width: var(--vro-loading-size);
59
+ height: var(--vro-loading-size);
60
+
61
+ i {
62
+ width: calc(var(--vro-loading-size) * 0.18);
63
+ height: calc(var(--vro-loading-size) * 0.18);
64
+ background: currentColor;
65
+ border-radius: 50%;
66
+ animation: vro-loading-dot 1s ease-in-out infinite;
67
+
68
+ &:nth-child(2) {
69
+ animation-delay: 0.15s;
70
+ }
71
+
72
+ &:nth-child(3) {
73
+ animation-delay: 0.3s;
74
+ }
75
+ }
76
+ }
77
+
78
+ @keyframes vro-loading-rotate {
79
+ 100% {
80
+ transform: rotate(360deg);
81
+ }
82
+ }
83
+
84
+ @keyframes vro-loading-circular {
85
+ 0% {
86
+ stroke-dasharray: 1, 200;
87
+ stroke-dashoffset: 0;
88
+ }
89
+
90
+ 50% {
91
+ stroke-dasharray: 90, 150;
92
+ stroke-dashoffset: -40;
93
+ }
94
+
95
+ 100% {
96
+ stroke-dasharray: 90, 150;
97
+ stroke-dashoffset: -120;
98
+ }
99
+ }
100
+
101
+ @keyframes vro-loading-fade {
102
+ 100% {
103
+ transform: rotate(360deg);
104
+ }
105
+ }
106
+
107
+ @keyframes vro-loading-dot {
108
+ 0%,
109
+ 80%,
110
+ 100% {
111
+ opacity: 0.35;
112
+ transform: scale(0.72);
113
+ }
114
+
115
+ 40% {
116
+ opacity: 1;
117
+ transform: scale(1);
118
+ }
119
+ }
@@ -0,0 +1 @@
1
+ import './css'
@@ -0,0 +1,39 @@
1
+ import type { ExtractPropTypes, PropType } from 'vue'
2
+
3
+ export type VroLoadingType = 'circular' | 'spinner' | 'dots'
4
+
5
+ export const vroLoadingProps = {
6
+ /**
7
+ * 加载图标类型。
8
+ */
9
+ type: {
10
+ type: String as PropType<VroLoadingType>,
11
+ default: 'circular',
12
+ },
13
+
14
+ /**
15
+ * 加载图标尺寸,传入数字时单位为 px。
16
+ */
17
+ size: {
18
+ type: [String, Number] as PropType<string | number>,
19
+ default: 24,
20
+ },
21
+
22
+ /**
23
+ * 加载图标颜色。
24
+ */
25
+ color: {
26
+ type: String,
27
+ default: 'currentColor',
28
+ },
29
+
30
+ /**
31
+ * 无障碍标签。
32
+ */
33
+ ariaLabel: {
34
+ type: String,
35
+ default: '加载中',
36
+ },
37
+ }
38
+
39
+ export type VroLoadingProps = ExtractPropTypes<typeof vroLoadingProps>
@@ -0,0 +1,39 @@
1
+ <template>
2
+ <div class="vro-loading" :style="loadingStyle" role="status" :aria-label="ariaLabel">
3
+ <slot name="icon">
4
+ <span v-if="type === 'spinner'" class="vro-loading__spinner" aria-hidden="true">
5
+ <i v-for="item in 12" :key="item"></i>
6
+ </span>
7
+
8
+ <span v-else-if="type === 'dots'" class="vro-loading__dots" aria-hidden="true">
9
+ <i v-for="item in 3" :key="item"></i>
10
+ </span>
11
+
12
+ <svg v-else class="vro-loading__circular" viewBox="0 0 50 50" aria-hidden="true">
13
+ <circle cx="25" cy="25" r="20" fill="none"></circle>
14
+ </svg>
15
+ </slot>
16
+ </div>
17
+ </template>
18
+
19
+ <script setup lang="ts">
20
+ import { computed } from 'vue'
21
+
22
+ import { vroLoadingProps } from './types'
23
+
24
+ defineOptions({ name: 'VroLoading' })
25
+
26
+ const props = defineProps(vroLoadingProps)
27
+
28
+ const addUnit = (value?: string | number) => {
29
+ if (typeof value === 'number') {
30
+ return `${value}px`
31
+ }
32
+ return value || undefined
33
+ }
34
+
35
+ const loadingStyle = computed(() => ({
36
+ '--vro-loading-size': addUnit(props.size),
37
+ '--vro-loading-color': props.color,
38
+ }))
39
+ </script>
@@ -0,0 +1,46 @@
1
+ # VroVnr
2
+
3
+ ### 介绍
4
+
5
+ 用于渲染由 `h`、`render` 函数或其他逻辑生成的 VNode 数组。
6
+
7
+ ## 代码演示
8
+
9
+ ### 基础用法
10
+
11
+ ```html
12
+ <template>
13
+ <vro-vnr :nodes="nodes" />
14
+ </template>
15
+
16
+ <script setup lang="ts">
17
+ import { h } from 'vue'
18
+
19
+ const nodes = [h('span', '文本'), h('button', { type: 'button' }, '按钮')]
20
+ </script>
21
+ ```
22
+
23
+ ## API
24
+
25
+ ### 属性 Props
26
+
27
+ <table>
28
+ <tbody>
29
+ <tr>
30
+ <td>名称</td>
31
+ <td>类型</td>
32
+ <td>默认值</td>
33
+ </tr>
34
+ <tr v-for="(item, key) in vroVnrProps" :key="key">
35
+ <td>{{ key }}</td>
36
+ <td>{{ parseType(item.type || item) }}</td>
37
+ <td>{{ reserve(item.default, '-') }}</td>
38
+ </tr>
39
+ </tbody>
40
+ </table>
41
+
42
+ <script setup lang="ts">
43
+ import { reserve } from '@daysnap/utils'
44
+ import { VroVnr, vroVnrProps } from '.'
45
+ import { parseType } from '../utils'
46
+ </script>
@@ -0,0 +1,15 @@
1
+ import { withInstall } from '../utils'
2
+ import Component from './vro-vnr.vue'
3
+
4
+ export * from './types'
5
+
6
+ export const VroVnr = withInstall<typeof Component>(Component)
7
+ export default VroVnr
8
+
9
+ export type VroVnrInstance = InstanceType<typeof VroVnr>
10
+
11
+ declare module 'vue' {
12
+ export interface GlobalComponents {
13
+ VroVnr: typeof VroVnr
14
+ }
15
+ }
@@ -0,0 +1,2 @@
1
+ import '../../style/base.scss'
2
+ import './index.scss'
File without changes
@@ -0,0 +1 @@
1
+ import './css'
@@ -0,0 +1,10 @@
1
+ import type { ExtractPropTypes, PropType, VNode } from 'vue'
2
+
3
+ export const vroVnrProps = {
4
+ nodes: {
5
+ type: Array as PropType<VNode[]>,
6
+ default: () => [],
7
+ },
8
+ }
9
+
10
+ export type VroVnrProps = ExtractPropTypes<typeof vroVnrProps>
@@ -0,0 +1,13 @@
1
+ <script lang="ts">
2
+ import { defineComponent } from 'vue'
3
+
4
+ import { vroVnrProps } from './types'
5
+
6
+ export default defineComponent({
7
+ name: 'VroVnr',
8
+ props: vroVnrProps,
9
+ setup(props) {
10
+ return () => props.nodes
11
+ },
12
+ })
13
+ </script>