Skip to content

feat: Loading component #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@
"chalk": "^5.0.1",
"cli-boxes": "^3.0.0",
"cli-cursor": "^4.0.0",
"cli-spinners": "^2.7.0",
"cli-truncate": "^3.1.0",
"indent-string": "^5.0.0",
"slice-ansi": "^5.0.0",
45 changes: 45 additions & 0 deletions packages/core/src/components/Loading.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
computed,
defineComponent,
h,
inject,
onMounted,
onUnmounted,
onUpdated,
ref,
} from '@vue/runtime-core'
import spinners from 'cli-spinners'
import type { PropType } from '@vue/runtime-core'
import { scheduleUpdateSymbol } from '../injectionSymbols'
import type { SpinnerName } from 'cli-spinners'

export const TuiLoading = defineComponent({
props: {
/**
* Type of a loading.
* See [cli-spinners](https://github.com/sindresorhus/cli-spinners) for available spinners.
*
* @default dots
*/
type: String as PropType<SpinnerName>,
},
setup(props) {
const spinner = computed(() => spinners[props.type ?? 'dots'])
const frame = ref(0)
let timer: NodeJS.Timer | null = null
const scheduleUpdate = inject(scheduleUpdateSymbol)!
onUpdated(scheduleUpdate)
onMounted(() => {
timer = setInterval(() => {
frame.value = (frame.value + 1) % spinner.value?.frames?.length
}, spinner.value.interval)
})
onUnmounted(() => {
clearInterval(timer!)
})

return () => {
return h('tui:text', spinner.value?.frames[frame.value])
}
},
})
2 changes: 1 addition & 1 deletion packages/core/src/components/TextTransform.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import chalk, { ForegroundColor } from 'chalk'
import { h, inject, FunctionalComponent } from '@vue/runtime-core'
import { scheduleUpdateSymbol } from '../injectionSymbols'
import type { OutputTransformer } from '../renderer/Output'
import { defaultStyle, TuiTextProps } from './Text'
import { colorize } from '../renderer/textColor'
import { scheduleUpdateSymbol } from '../injectionSymbols'

/**
* A Text Transforms allows modifying the text before it is written to the stdout while accounting for line breaks and
1 change: 1 addition & 0 deletions packages/core/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -5,4 +5,5 @@ export { TuiApp } from './App'
export { TuiBox } from './Box'

export { TuiLink } from './Link'
export { TuiLoading } from './Loading'
// export { default as TuiInput } from './Input.vue'
2 changes: 2 additions & 0 deletions packages/playground/components.d.ts
Original file line number Diff line number Diff line change
@@ -7,8 +7,10 @@ export {}

declare module '@vue/runtime-core' {
export interface GlobalComponents {
Box: typeof import('vue-termui')['TuiBox']
Div: typeof import('vue-termui')['TuiBox']
Link: typeof import('vue-termui')['TuiLink']
Loading: typeof import('vue-termui')['TuiLoading']
Text: typeof import('vue-termui')['TuiText']
}
}
8 changes: 8 additions & 0 deletions packages/playground/src/LoadingDemo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template>
<Box :margin="5" width="20" border-style="round" justify-content="center">
<Text color="yellow">
<Loading type="clock" />
<Text color="white"> Hello World </Text>
</Text>
</Box>
</template>
3 changes: 2 additions & 1 deletion packages/playground/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// import devtools from '@vue/devtools'
// import devtools from '@vue/devtools/node'
import { createApp } from 'vue-termui'
import App from './Focusables.vue'
// import App from './Focusables.vue'
import App from './LoadingDemo.vue'
// import App from './Fragments.vue'
// import App from './CenteredDemo.vue'
// import App from './App.vue'
1 change: 1 addition & 0 deletions packages/playground/vite.config.cjs
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ export default defineConfig({
'yoga-layout-prebuilt',
'is-fullwidth-code-point',
'terminal-link',
'cli-spinners',
'restore-cursor',
],
},
4 changes: 4 additions & 0 deletions packages/vite-plugin-vue-termui/src/index.ts
Original file line number Diff line number Diff line change
@@ -116,6 +116,8 @@ export default function VueTermui({
'wrap-ansi',
'yoga-layout',
'yoga-layout-prebuilt',
'terminal-link',
'cli-spinners',
],

output: {
@@ -169,6 +171,8 @@ export const VueTuiComponents = new Map<string, ModuleExports>([
['a', 'TuiLink'],
['link', 'TuiLink'],

['loading', 'TuiLoading'],

['div', 'TuiBox'],
['box', 'TuiBox'],

7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.