Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Convert target index files to TypeScript
  • Loading branch information
jacobrask committed Feb 19, 2019
commit 428f39588b52cea8562cfa7ba180579d0bf8f3a3
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"react": "16.8.1",
"react-dom": "16.8.1",
"react-konva": "^16.8.0",
"react-native": "^0.58.4",
"react-test-renderer": "16.8.1",
"react-testing-library": "5.6.1",
"rimraf": "2.6.3",
Expand Down Expand Up @@ -127,7 +128,9 @@
"src/**/*.js",
"!test/"
],
"setupFilesAfterEnv": ["<rootDir>/setupTests.js"]
"setupFilesAfterEnv": [
"<rootDir>/setupTests.js"
]
},
"collective": {
"type": "opencollective",
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const getBabelOptions = ({ useESModules }, targets) => ({
function createConfig(entry, out) {
return [
{
input: `./src/${entry}/index.js`,
input: `./src/${entry}/index`,
output: { file: `dist/${out}.js`, format: 'esm' },
external,
plugins: [
Expand All @@ -44,7 +44,7 @@ function createConfig(entry, out) {
],
},
{
input: `./src/${entry}/index.js`,
input: `./src/${entry}/index`,
output: { file: `dist/${out}.cjs.js`, format: 'cjs' },
external,
plugins: [
Expand Down
28 changes: 15 additions & 13 deletions src/animated/Globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { MutableRefObject, ReactType } from 'react'
import createInterpolation from '../shared/interpolation'
import AnimatedStyle from './AnimatedStyle'

interface ApplyAnimatedValues {
fn(node?: any, props?: any): undefined | false
transform<Style extends object>(style: Style): Style
type ApplyPropsFunction = (node?: any, props?: any) => undefined | false
type TransformFunction = (style: any) => any
export let applyAnimatedValues: {
fn: ApplyPropsFunction
transform: TransformFunction
}
export let applyAnimatedValues: ApplyAnimatedValues
export function injectApplyAnimatedValues(
fn: ApplyAnimatedValues['fn'],
transform: ApplyAnimatedValues['transform']
fn: ApplyPropsFunction,
transform: TransformFunction
) {
applyAnimatedValues = { fn, transform }
}
Expand Down Expand Up @@ -45,15 +46,16 @@ export function injectDefaultElement(el?: typeof defaultElement) {
}

interface AnimatedApi {
<T extends ReactType>(node: MutableRefObject<T>): T
<T extends ReactType>(
node: MutableRefObject<T>,
mounted: MutableRefObject<boolean>,
forceUpdate: () => void
): {
getNode(): T
setNativeProps(props: any): void
}
mounted?: MutableRefObject<boolean>,
forceUpdate?: () => void
):
| T
| {
getNode(): T
setNativeProps(props: any): void
}
}
export let animatedApi: AnimatedApi = (node: any) => node.current
export function injectAnimatedApi(fn: typeof animatedApi) {
Expand Down
17 changes: 15 additions & 2 deletions src/animated/createAnimatedComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, {
ComponentPropsWithRef,
forwardRef,
ForwardRefExoticComponent,
MutableRefObject,
PropsWithoutRef,
ReactType,
RefAttributes,
useCallback,
useEffect,
useImperativeHandle,
Expand Down Expand Up @@ -38,9 +41,17 @@ type AnimatedComponentProps<C extends ReactType> = JSX.LibraryManagedAttributes<
AnimateStyleProp<ComponentPropsWithRef<C>> & ScrollProps
>

export default function createAnimatedComponent<C extends ReactType>(
export interface CreateAnimatedComponent<C extends ReactType> {
(Component: C): ForwardRefExoticComponent<
PropsWithoutRef<AnimatedComponentProps<C>> & RefAttributes<C>
>
}

const createAnimatedComponent: CreateAnimatedComponent<ReactType> = <
C extends ReactType
>(
Component: C
) {
) => {
const AnimatedComponent = forwardRef<C, AnimatedComponentProps<C>>(
(props, ref) => {
const forceUpdate = useForceUpdate()
Expand Down Expand Up @@ -89,3 +100,5 @@ export default function createAnimatedComponent<C extends ReactType>(
)
return AnimatedComponent
}

export default createAnimatedComponent
2 changes: 2 additions & 0 deletions src/modules.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @types/react-native introduces lots of global conflicts
declare module 'react-native'
74 changes: 46 additions & 28 deletions src/targets/konva/index.js → src/targets/konva/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import * as Globals from '../../animated/Globals'
import Controller from '../../animated/Controller'
import { ReactType } from 'react'
import * as konva from 'react-konva'
import { interpolate } from '../../animated/AnimatedInterpolation'
import animated from '../../animated/createAnimatedComponent'
import createInterpolation from '../../shared/interpolation'
import animated, {
CreateAnimatedComponent,
} from '../../animated/createAnimatedComponent'
import * as Globals from '../../animated/Globals'
import colorNames from '../../shared/colors'
import { config } from '../../shared/constants'
import createInterpolation from '../../shared/interpolation'
import { useChain } from '../../useChain'
import { useSpring } from '../../useSpring'
import { useSprings } from '../../useSprings'
import { useTrail } from '../../useTrail'
import { useTransition } from '../../useTransition'
import { useChain } from '../../useChain'
import { useSprings } from '../../useSprings'

Globals.injectDefaultElement('Group')
Globals.injectInterpolation(createInterpolation)
Expand All @@ -18,47 +21,62 @@ Globals.injectApplyAnimatedValues(
(instance, props) => {
if (instance.nodeType) {
instance._applyProps(instance, props)
return
} else return false
},
style => style
)

const konvaElements = [
'Layer',
type KonvaComponents = Pick<
typeof konva,
{
[K in keyof typeof konva]: typeof konva[K] extends ReactType ? K : never
}[keyof typeof konva]
>

const konvaElements: (keyof KonvaComponents)[] = [
'Arc',
'Arrow',
'Circle',
'Ellipse',
'FastLayer',
'Group',
'Image',
'Label',
'Rect',
'Circle',
'Ellipse',
'Wedge',
'Layer',
'Line',
'Sprite',
'Image',
'Text',
'TextPath',
'Star',
'Ring',
'Arc',
'Tag',
'Path',
'Rect',
'RegularPolygon',
'Arrow',
'Ring',
'Shape',
'Sprite',
'Star',
'Tag',
'Text',
'TextPath',
'Transformer',
'Wedge',
]

Object.assign(
animated,
konvaElements.reduce(
(acc, element) => ({ ...acc, [element]: animated(element) }),
{}
)
type AnimatedWithKonvaElements = CreateAnimatedComponent<ReactType> &
{
[Tag in keyof KonvaComponents]: ReturnType<
CreateAnimatedComponent<KonvaComponents[Tag]>
>
}

const extendedAnimated = konvaElements.reduce(
(acc, element) => {
acc[element] = animated(element as ReactType)
return acc
},
animated as AnimatedWithKonvaElements
)

export {
config,
animated,
extendedAnimated as animated,
interpolate,
Globals,
useSpring,
Expand Down
17 changes: 8 additions & 9 deletions src/targets/native/index.js → src/targets/native/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import * as Globals from '../../animated/Globals'
import Controller from '../../animated/Controller'
import { StyleSheet, View } from 'react-native'
import { interpolate } from '../../animated/AnimatedInterpolation'
import AnimatedStyle from '../../animated/AnimatedStyle'
import animated from '../../animated/createAnimatedComponent'
import * as Globals from '../../animated/Globals'
import colorNames from '../../shared/colors'
import { config } from '../../shared/constants'
import AnimatedStyle from '../../animated/AnimatedStyle'
import createInterpolation from '../../shared/interpolation'
import colorNames from '../../shared/colors'
import AnimatedTransform from './AnimatedTransform'
import { StyleSheet, View } from 'react-native'
import { useChain } from '../../useChain'
import { useSpring } from '../../useSpring'
import { useSprings } from '../../useSprings'
import { useTrail } from '../../useTrail'
import { useTransition } from '../../useTransition'
import { useChain } from '../../useChain'
import { useSprings } from '../../useSprings'
import AnimatedTransform from './AnimatedTransform'

Globals.injectDefaultElement(View)
Globals.injectInterpolation(createInterpolation)
Expand All @@ -28,7 +27,7 @@ Globals.injectCreateAnimatedStyle(
Globals.injectAnimatedApi((node, mounted, forceUpdate) => ({
setNativeProps: props => {
const didUpdate = Globals.applyAnimatedValues.fn(node.current, props)
if (!didUpdate) mounted.current && forceUpdate()
if (!didUpdate) mounted!.current && forceUpdate!()
},
getNode: () => node.current,
}))
Expand Down
27 changes: 16 additions & 11 deletions src/targets/universal/index.js → src/targets/universal/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
import * as Globals from '../../animated/Globals'
import Controller from '../../animated/Controller'
import { interpolate } from '../../animated/AnimatedInterpolation'
import animated from '../../animated/createAnimatedComponent'
import * as Globals from '../../animated/Globals'
import Interpolation, {
InterpolationConfig,
} from '../../animated/Interpolation'
import { config } from '../../shared/constants'
import { useChain } from '../../useChain'
import { useSpring } from '../../useSpring'
import { useSprings } from '../../useSprings'
import { useTrail } from '../../useTrail'
import { useTransition } from '../../useTransition'
import { useChain } from '../../useChain'
import { useSprings } from '../../useSprings'

// Problem: https://github.com/animatedjs/animated/pull/102
// Solution: https://stackoverflow.com/questions/638565/parsing-scientific-notation-sensibly/658662
const stringShapeRegex = /[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?/g
function createInterpolation(config) {
function createInterpolation(config: InterpolationConfig<number, string>) {
const outputRange = config.output
const outputRanges = outputRange[0].match(stringShapeRegex).map(() => [])
const outputRanges: number[][] = outputRange[0]
.match(stringShapeRegex)!
.map(() => [])
outputRange.forEach(value => {
value
.match(stringShapeRegex)
.match(stringShapeRegex)!
.forEach((number, i) => outputRanges[i].push(+number))
})
const interpolations = outputRange[0]
.match(stringShapeRegex)
.match(stringShapeRegex)!
.map((_, i) => Interpolation.create({ ...config, output: outputRanges[i] }))
return input => {
return (input: number) => {
let i = 0
return outputRange[0].replace(stringShapeRegex, () =>
interpolations[i++](input)
return outputRange[0].replace(
stringShapeRegex,
() => interpolations[i++](input) as string
)
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/targets/web/globals.js → src/targets/web/globals.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as Globals from '../../animated/Globals'
import AnimatedStyle from '../../animated/AnimatedStyle'
import * as Globals from '../../animated/Globals'
import colorNames from '../../shared/colors'
import createInterpolation from '../../shared/interpolation'

let isUnitlessNumber = {
let isUnitlessNumber: { [key: string]: true } = {
animationIterationCount: true,
borderImageOutset: true,
borderImageSlice: true,
Expand Down Expand Up @@ -48,7 +48,7 @@ let isUnitlessNumber = {
strokeWidth: true,
}

const prefixKey = (prefix, key) =>
const prefixKey = (prefix: string, key: string) =>
prefix + key.charAt(0).toUpperCase() + key.substring(1)
const prefixes = ['Webkit', 'Ms', 'Moz', 'O']

Expand All @@ -57,7 +57,11 @@ isUnitlessNumber = Object.keys(isUnitlessNumber).reduce((acc, prop) => {
return acc
}, isUnitlessNumber)

function dangerousStyleValue(name, value, isCustomProperty) {
function dangerousStyleValue(
name: string,
value: string | number | boolean | null,
isCustomProperty: boolean
) {
if (value == null || typeof value === 'boolean' || value === '') return ''
if (
!isCustomProperty &&
Expand All @@ -70,7 +74,7 @@ function dangerousStyleValue(name, value, isCustomProperty) {
return ('' + value).trim()
}

const attributeCache = {}
const attributeCache: { [key: string]: string } = {}
Globals.injectCreateAnimatedStyle(style => new AnimatedStyle(style))
Globals.injectDefaultElement('div')
Globals.injectInterpolation(createInterpolation)
Expand Down Expand Up @@ -116,6 +120,7 @@ Globals.injectApplyAnimatedValues(
if (typeof instance.getAttribute(dashCase) !== 'undefined')
instance.setAttribute(dashCase, attributes[name])
}
return
} else return false
},
style => style
Expand Down
Loading