Skip to content
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

refactor(types): use template literal types insteads of any #4166

Merged
merged 2 commits into from
Oct 26, 2022
Merged
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
31 changes: 22 additions & 9 deletions packages/runtime-dom/src/components/Transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import { isObject, toNumber, extend, isArray } from '@vue/shared'
const TRANSITION = 'transition'
const ANIMATION = 'animation'

type AnimationTypes = typeof TRANSITION | typeof ANIMATION

export interface TransitionProps extends BaseTransitionProps<Element> {
name?: string
type?: typeof TRANSITION | typeof ANIMATION
type?: AnimationTypes
css?: boolean
duration?: number | { enter: number; leave: number }
// custom transition classes
Expand Down Expand Up @@ -358,24 +360,33 @@ function whenTransitionEnds(
}

interface CSSTransitionInfo {
type: typeof TRANSITION | typeof ANIMATION | null
type: AnimationTypes | null
propCount: number
timeout: number
hasTransform: boolean
}

type AnimationProperties = 'Delay' | 'Duration'
type StylePropertiesKey =
| `${AnimationTypes}${AnimationProperties}`
| `${typeof TRANSITION}Property`

export function getTransitionInfo(
el: Element,
expectedType?: TransitionProps['type']
): CSSTransitionInfo {
const styles: any = window.getComputedStyle(el)
const styles = window.getComputedStyle(el) as Pick<
CSSStyleDeclaration,
StylePropertiesKey
>
// JSDOM may return undefined for transition properties
const getStyleProperties = (key: string) => (styles[key] || '').split(', ')
const transitionDelays = getStyleProperties(TRANSITION + 'Delay')
const transitionDurations = getStyleProperties(TRANSITION + 'Duration')
const getStyleProperties = (key: StylePropertiesKey) =>
(styles[key] || '').split(', ')
const transitionDelays = getStyleProperties(`${TRANSITION}Delay`)
const transitionDurations = getStyleProperties(`${TRANSITION}Duration`)
const transitionTimeout = getTimeout(transitionDelays, transitionDurations)
const animationDelays = getStyleProperties(ANIMATION + 'Delay')
const animationDurations = getStyleProperties(ANIMATION + 'Duration')
const animationDelays = getStyleProperties(`${ANIMATION}Delay`)
const animationDurations = getStyleProperties(`${ANIMATION}Duration`)
const animationTimeout = getTimeout(animationDelays, animationDurations)

let type: CSSTransitionInfo['type'] = null
Expand Down Expand Up @@ -410,7 +421,9 @@ export function getTransitionInfo(
}
const hasTransform =
type === TRANSITION &&
/\b(transform|all)(,|$)/.test(styles[TRANSITION + 'Property'])
/\b(transform|all)(,|$)/.test(
getStyleProperties(`${TRANSITION}Property`).toString()
)
return {
type,
timeout,
Expand Down