refactor(alert): ts

This commit is contained in:
07akioni 2021-01-15 00:35:19 +08:00
parent 3b1c7d2c17
commit b907e295d7
7 changed files with 99 additions and 62 deletions

View File

@ -37,8 +37,8 @@
</n-fade-in-expand-transition> </n-fade-in-expand-transition>
</template> </template>
<script> <script lang="ts">
import { ref, computed, defineComponent } from 'vue' import { ref, computed, defineComponent, PropType } from 'vue'
import { import {
InfoIcon, InfoIcon,
SuccessIcon, SuccessIcon,
@ -48,7 +48,8 @@ import {
import { NFadeInExpandTransition, NBaseClose, NBaseIcon } from '../../_base' import { NFadeInExpandTransition, NBaseClose, NBaseIcon } from '../../_base'
import { useTheme } from '../../_mixins' import { useTheme } from '../../_mixins'
import { warn, createKey } from '../../_utils' import { warn, createKey } from '../../_utils'
import { alertLight } from '../styles' import { alertLight } from '../styles/index'
import type { AlertThemeVars } from '../styles/index'
import style from './styles/index.cssr' import style from './styles/index.cssr'
export default defineComponent({ export default defineComponent({
@ -74,9 +75,9 @@ export default defineComponent({
default: true default: true
}, },
type: { type: {
validator (type) { type: String as PropType<
return ['info', 'warning', 'error', 'success', 'default'].includes(type) 'info' | 'warning' | 'error' | 'success' | 'default'
}, >,
default: 'default' default: 'default'
}, },
closable: { closable: {
@ -92,7 +93,8 @@ export default defineComponent({
default: undefined default: undefined
}, },
onAfterHide: { onAfterHide: {
validator () { type: Function,
validator: () => {
if (__DEV__) { if (__DEV__) {
warn( warn(
'alert', 'alert',
@ -105,7 +107,13 @@ export default defineComponent({
} }
}, },
setup (props) { setup (props) {
const themeRef = useTheme('Alert', 'Alert', style, alertLight, props) const themeRef = useTheme<AlertThemeVars>(
'Alert',
'Alert',
style,
alertLight,
props
)
const cssVars = computed(() => { const cssVars = computed(() => {
const { const {
common: { cubicBezierEaseInOut }, common: { cubicBezierEaseInOut },

View File

@ -15,37 +15,46 @@ import fadeInHeightExpandTranstion from '../../../_styles/transitions/fade-in-he
// --border-radius // --border-radius
// --font-size // --font-size
// --title-font-weight // --title-font-weight
export default cB('alert', ` export default cB(
'alert',
`
line-height: var(--line-height); line-height: var(--line-height);
border-radius: var(--border-radius); border-radius: var(--border-radius);
position: relative; position: relative;
transition: background-color .3s var(--bezier); transition: background-color .3s var(--bezier);
background-color: var(--color); background-color: var(--color);
text-align: start; text-align: start;
`, [ `,
cE('icon', { [
color: 'var(--icon-color)' cE('icon', {
}), color: 'var(--icon-color)'
cB('alert-body', {
border: 'var(--border)'
}, [
cE('title', {
color: 'var(--title-text-color)'
}), }),
cE('content', { cB(
color: 'var(--content-text-color)' 'alert-body',
}) {
]), border: 'var(--border)'
fadeInHeightExpandTranstion({ },
originalTransition: 'transform .3s var(--bezier)', [
enterToProps: { cE('title', {
transform: 'scale(1)' color: 'var(--title-text-color)'
}, }),
leaveToProps: { cE('content', {
transform: 'scale(0.9)' color: 'var(--content-text-color)'
} })
}), ]
cE('icon', ` ),
fadeInHeightExpandTranstion({
originalTransition: 'transform .3s var(--bezier)',
enterToProps: {
transform: 'scale(1)'
},
leaveToProps: {
transform: 'scale(0.9)'
}
}),
cE(
'icon',
`
position: absolute; position: absolute;
left: 12px; left: 12px;
top: 14px; top: 14px;
@ -54,42 +63,55 @@ export default cB('alert', `
width: 26px; width: 26px;
height: 26px; height: 26px;
font-size: 26px; font-size: 26px;
`), `
cE('close', ` ),
cE(
'close',
`
transition: color .3s var(--bezier); transition: color .3s var(--bezier);
position: absolute; position: absolute;
right: 16px; right: 16px;
font-size: 14px; font-size: 14px;
top: 14px; top: 14px;
`), `
cB('alert-body', ` ),
cB(
'alert-body',
`
border-radius: var(--border-radius); border-radius: var(--border-radius);
padding: 15px 15px 15px 47px; padding: 15px 15px 15px 47px;
transition: border-color .3s var(--bezier); transition: border-color .3s var(--bezier);
`, [ `,
cE('title', ` [
cE(
'title',
`
transition: color .3s var(--bezier); transition: color .3s var(--bezier);
font-size: 16px; font-size: 16px;
line-height: 19px; line-height: 19px;
font-weight: var(--title-font-weight); font-weight: var(--title-font-weight);
`, [ `,
c('& +', [ [
c('& +', [
cE('content', {
marginTop: '9px'
})
])
]
),
cE('content', { cE('content', {
marginTop: '9px' transition: 'color .3s var(--bezier)',
fontSize: 'var(--font-size)'
}) })
]) ]
]), ),
cE('content', { cE('icon', {
transition: 'color .3s var(--bezier)', transition: 'color .3s var(--bezier)'
fontSize: 'var(--font-size)' }),
}) cM('no-icon', [
]), cB('alert-body', {
cE('icon', { paddingLeft: '19px'
transition: 'color .3s var(--bezier)' })
}), ])
cM('no-icon', [ ]
cB('alert-body', { )
paddingLeft: '19px'
})
])
])

View File

@ -1,10 +1,12 @@
import { changeColor } from 'seemly' import { changeColor } from 'seemly'
import { commonDark } from '../../_styles/new-common' import { commonDark } from '../../_styles/new-common'
import type { ThemeCommonVars } from '../../_styles/new-common'
import type { AlertThemeVars } from './light'
export default { export default {
name: 'Alert', name: 'Alert',
common: commonDark, common: commonDark,
self (vars) { self (vars: ThemeCommonVars): AlertThemeVars {
const { const {
lineHeight, lineHeight,
borderRadius, borderRadius,

View File

@ -1,2 +0,0 @@
export { default as alertDark } from './dark.js'
export { default as alertLight } from './light.js'

View File

@ -0,0 +1,3 @@
export { default as alertDark } from './dark'
export { default as alertLight } from './light'
export type { AlertThemeVars } from './light'

View File

@ -1,10 +1,11 @@
import { changeColor, composite } from 'seemly' import { changeColor, composite } from 'seemly'
import { commonLight } from '../../_styles/new-common' import { commonLight } from '../../_styles/new-common'
import type { ThemeCommonVars } from '../../_styles/new-common'
export default { const alertLight = {
name: 'Alert', name: 'Alert',
common: commonLight, common: commonLight,
self (vars) { self (vars: ThemeCommonVars) {
const { const {
lineHeight, lineHeight,
borderRadius, borderRadius,
@ -92,3 +93,6 @@ export default {
} }
} }
} }
export default alertLight
export type AlertThemeVars = ReturnType<typeof alertLight.self>