Skip to content

Commit

Permalink
fix(types): improve with strict mode (#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincanac committed Nov 30, 2023
1 parent 464ff0b commit 4a9b66a
Show file tree
Hide file tree
Showing 68 changed files with 266 additions and 242 deletions.
1 change: 0 additions & 1 deletion .nuxtrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
imports.autoImport=false
typescript.includeWorkspace=true
typescript.strict=false
3 changes: 3 additions & 0 deletions docs/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,8 @@ export default defineNuxtConfig({
}
})
}
},
typescript: {
strict: false
}
})
4 changes: 1 addition & 3 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import module from '../src/module'

export default defineNuxtConfig({
modules: [
module
'../src/module'
]
})
2 changes: 1 addition & 1 deletion src/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export const generateSafelist = (colors: string[], globalColors) => {
}

export const customSafelistExtractor = (prefix, content: string, colors: string[], safelistColors: string[]) => {
const classes = []
const classes: string[] = []
const regex = /<([A-Za-z][A-Za-z0-9]*(?:-[A-Za-z][A-Za-z0-9]*)*)\s+(?![^>]*:color\b)[^>]*\bcolor=["']([^"']+)["'][^>]*>/gs

const matches = content.matchAll(regex)
Expand Down
12 changes: 10 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import type { DeepPartial, Strategy } from './runtime/types/utils'

const defaultExtractor = createDefaultExtractor({ tailwindConfig: { separator: ':' } })

// @ts-ignore
delete defaultColors.lightBlue
// @ts-ignore
delete defaultColors.warmGray
// @ts-ignore
delete defaultColors.trueGray
// @ts-ignore
delete defaultColors.coolGray
// @ts-ignore
delete defaultColors.blueGray

type UI = {
Expand Down Expand Up @@ -79,12 +84,15 @@ export default defineNuxtModule<ModuleOptions>({

// @ts-ignore
nuxt.hook('tailwindcss:config', function (tailwindConfig) {
tailwindConfig.theme = tailwindConfig.theme || {}
tailwindConfig.theme.extend = tailwindConfig.theme.extend || {}
tailwindConfig.theme.extend.colors = tailwindConfig.theme.extend.colors || {}

const globalColors: any = {
...(tailwindConfig.theme.colors || defaultColors),
...tailwindConfig.theme.extend?.colors
}

tailwindConfig.theme.extend.colors = tailwindConfig.theme.extend.colors || {}
// @ts-ignore
globalColors.primary = tailwindConfig.theme.extend.colors.primary = {
50: 'rgb(var(--color-primary-50) / <alpha-value>)',
Expand Down Expand Up @@ -132,7 +140,7 @@ export default defineNuxtModule<ModuleOptions>({
}

tailwindConfig.safelist = tailwindConfig.safelist || []
tailwindConfig.safelist.push(...generateSafelist(options.safelistColors, colors))
tailwindConfig.safelist.push(...generateSafelist(options.safelistColors || [], colors))

tailwindConfig.plugins = tailwindConfig.plugins || []
tailwindConfig.plugins.push(iconsPlugin(Array.isArray(options.icons) || options.icons === 'all' ? { collections: getIconCollections(options.icons) } : typeof options.icons === 'object' ? options.icons as IconsPluginOptions : {}))
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/components/data/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<slot :name="`${column.key}-header`" :column="column" :sort="sort" :on-sort="onSort">
<UButton
v-if="column.sortable"
v-bind="{ ...ui.default.sortButton, ...sortButton }"
v-bind="{ ...(ui.default.sortButton || {}), ...sortButton }"
:icon="(!sort.column || sort.column !== column.key) ? (sortButton.icon || ui.default.sortButton.icon) : sort.direction === 'asc' ? sortAscIcon : sortDescIcon"
:label="column[columnAttribute]"
@click="onSort(column)"
Expand Down Expand Up @@ -145,11 +145,11 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
emits: ['update:modelValue', 'update:sort'],
Expand Down
10 changes: 5 additions & 5 deletions src/runtime/components/elements/Accordion.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
setup (props) {
const { ui, attrs } = useUI('accordion', toRef(props, 'ui'), config, toRef(props, 'class'))
const uiButton = computed<Partial<typeof configButton>>(() => configButton)
const uiButton = computed<typeof configButton>(() => configButton)
const buttonRefs = ref<{ open: boolean, close: (e: EventTarget) => {} }[]>([])
Expand All @@ -114,7 +114,7 @@ export default defineComponent({
buttonRefs.value.forEach((button) => {
if (button.open) {
button.close(e.target)
button.close(e.target as EventTarget)
}
})
}
Expand Down
14 changes: 7 additions & 7 deletions src/runtime/components/elements/Alert.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
</p>

<div v-if="(description || $slots.description) && actions.length" :class="ui.actions">
<UButton v-for="(action, index) of actions" :key="index" v-bind="{ ...ui.default.actionButton, ...action }" @click.stop="action.click" />
<UButton v-for="(action, index) of actions" :key="index" v-bind="{ ...(ui.default.actionButton || {}), ...action }" @click.stop="action.click" />
</div>
</div>
<div v-if="closeButton || (!description && !$slots.description && actions.length)" :class="twMerge(ui.actions, 'mt-0')">
<template v-if="!description && !$slots.description && actions.length">
<UButton v-for="(action, index) of actions" :key="index" v-bind="{ ...ui.default.actionButton, ...action }" @click.stop="action.click" />
<UButton v-for="(action, index) of actions" :key="index" v-bind="{ ...(ui.default.actionButton || {}), ...action }" @click.stop="action.click" />
</template>

<UButton v-if="closeButton" aria-label="Close" v-bind="{ ...ui.default.closeButton, ...closeButton }" @click.stop="$emit('close')" />
<UButton v-if="closeButton" aria-label="Close" v-bind="{ ...(ui.default.closeButton || {}), ...closeButton }" @click.stop="$emit('close')" />
</div>
</div>
</div>
Expand Down Expand Up @@ -73,7 +73,7 @@ export default defineComponent({
},
closeButton: {
type: Object as PropType<Button>,
default: () => config.default.closeButton as Button
default: () => config.default.closeButton as unknown as Button
},
actions: {
type: Array as PropType<(Button & { click?: Function })[]>,
Expand All @@ -98,11 +98,11 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
emits: ['close'],
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/components/elements/Avatar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
setup (props) {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/components/elements/AvatarGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof avatarGroupConfig & { strategy?: Strategy }>>,
default: undefined
default: () => ({})
}
},
setup (props, { slots }) {
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/components/elements/Badge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
setup (props) {
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/components/elements/Button.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
setup (props, { slots }) {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/components/elements/ButtonGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof buttonGroupConfig & { strategy?: Strategy }>>,
default: undefined
default: () => ({})
}
},
setup (props, { slots }) {
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/components/elements/Chip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
setup (props) {
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/components/elements/Dropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
setup (props) {
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/components/elements/Kbd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
setup (props) {
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/components/elements/Meter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
setup (props) {
Expand Down
10 changes: 5 additions & 5 deletions src/runtime/components/elements/MeterGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof meterGroupConfig & { strategy?: Strategy }>>,
default: undefined
default: () => ({})
}
},
setup (props, { slots }) {
Expand Down Expand Up @@ -128,7 +128,7 @@ export default defineComponent({
vProps.ui.wrapper = node.props?.ui?.wrapper || ''
vProps.ui.wrapper += [
node.props?.ui?.wrapper,
props.ui?.meter?.background || ui.value.background,
ui.value.background,
ui.value.transition
].filter(Boolean).join(' ')

Expand All @@ -153,8 +153,8 @@ export default defineComponent({

// @ts-expect-error
delete(clone.children?.label)
delete(clone.props.indicator)
delete(clone.props.label)
delete(clone.props?.indicator)
delete(clone.props?.label)

return clone
}))
Expand Down
16 changes: 8 additions & 8 deletions src/runtime/components/elements/Progress.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</div>
</slot>

<progress :class="progressClass" v-bind="{ value, max: realMax }">
<progress :class="progressClass" v-bind="{ value, max: (realMax as number) }">
{{ Math.round(percent) }}%
</progress>

Expand Down Expand Up @@ -39,7 +39,7 @@ export default defineComponent({
inheritAttrs: false,
props: {
value: {
type: [Number, null, undefined],
type: Number,
default: null
},
max: {
Expand Down Expand Up @@ -73,11 +73,11 @@ export default defineComponent({
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
setup (props) {
Expand Down Expand Up @@ -173,7 +173,7 @@ export default defineComponent({
return classes.join(' ')
}
const isIndeterminate = computed(() => [undefined, null].includes(props.value))
const isIndeterminate = computed(() => props.value === undefined || props.value === null)
const isSteps = computed(() => Array.isArray(props.max))
const realMax = computed(() => {
Expand All @@ -191,8 +191,8 @@ export default defineComponent({
const percent = computed(() => {
switch (true) {
case props.value < 0: return 0
case props.value > realMax.value: return 100
default: return (props.value / realMax.value) * 100
case props.value > (realMax.value as number): return 100
default: return (props.value / (realMax.value as number)) * 100
}
})
Expand Down
Loading

1 comment on commit 4a9b66a

@vercel
Copy link

@vercel vercel bot commented on 4a9b66a Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./

ui.nuxt.com
ui-git-dev-nuxt-js.vercel.app
ui-nuxt-js.vercel.app

Please sign in to comment.