-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for React Native Aniamted
- Loading branch information
Showing
12 changed files
with
187 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"main": "../../../lib/commonjs/components/native/Animated.js", | ||
"module": "../../../lib/module/components/native/Animated.js", | ||
"react-native": "../../../src/components/native/Animated.tsx" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const babel = require('@babel/core') | ||
const plugin = require('../index.js') | ||
|
||
const filePath = '../../expo-example/app/(tabs)/index.tsx' | ||
|
||
const result = babel.transformFileSync(filePath, { | ||
presets: ['@babel/preset-typescript', '@babel/preset-flow'], | ||
plugins: [plugin], | ||
filename: filePath, | ||
}) | ||
|
||
console.log(result.code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const REACT_NATIVE_COMPONENT_NAMES = [ | ||
'ActivityIndicator', | ||
'View', | ||
'Text', | ||
'Image', | ||
'ImageBackground', | ||
'KeyboardAvoidingView', | ||
'Pressable', | ||
'ScrollView', | ||
'FlatList', | ||
'SectionList', | ||
'Switch', | ||
'TextInput', | ||
'RefreshControl', | ||
'TouchableHighlight', | ||
'TouchableOpacity', | ||
'VirtualizedList', | ||
'Animated' | ||
// Modal - there is no exposed native handle | ||
// TouchableWithoutFeedback - can't accept a ref | ||
] | ||
|
||
// auto replace RN imports to Unistyles imports under these paths | ||
// our implementation simply borrows 'ref' to register it in ShadowRegistry | ||
// so we won't affect anyone's implementation | ||
const REPLACE_WITH_UNISTYLES_PATHS = [ | ||
'react-native-reanimated/src/component', | ||
'react-native-gesture-handler/src/components' | ||
] | ||
|
||
// this is more powerful API as it allows to convert unmatched imports to Unistyles | ||
// { path: string, imports: Array<{ name: string, isDefault: boolean, path: string, mapTo: string }> } | ||
// name <- target import name | ||
// isDefault <- is the import default? | ||
// path <- path to the target import | ||
// mapTo <- name of the Unistyles component | ||
const REPLACE_WITH_UNISTYLES_EXOTIC_PATHS = [] | ||
|
||
module.exports = { | ||
REACT_NATIVE_COMPONENT_NAMES, | ||
REPLACE_WITH_UNISTYLES_PATHS, | ||
REPLACE_WITH_UNISTYLES_EXOTIC_PATHS | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
function handleExoticImport(t, path, state, exoticImport) { | ||
const specifiers = path.node.specifiers | ||
const source = path.node.source | ||
|
||
if (path.node.importKind !== 'value') { | ||
return | ||
} | ||
|
||
specifiers.forEach(specifier => { | ||
for (const rule of exoticImport.imports) { | ||
const hasMatchingImportType = !rule.isDefault || t.isImportDefaultSpecifier(specifier) | ||
const hasMatchingImportName = rule.name === specifier.local.name | ||
const hasMatchingPath = rule.path === source.value | ||
|
||
if (!hasMatchingImportType || !hasMatchingImportName || !hasMatchingPath) { | ||
continue | ||
} | ||
|
||
const newImport = t.importDeclaration( | ||
[t.importSpecifier(t.identifier(rule.mapTo), t.identifier(rule.mapTo))], | ||
t.stringLiteral(state.opts.isLocal | ||
? state.file.opts.filename.split('react-native-unistyles').at(0).concat(`react-native-unistyles/components/native/${rule.mapTo}`) | ||
: `react-native-unistyles/components/native/${rule.mapTo}` | ||
) | ||
) | ||
|
||
// remove old import | ||
if (t.isImportDefaultSpecifier(specifier)) { | ||
path.replaceWith(newImport) | ||
} else { | ||
path.node.specifiers = specifiers.filter(s => s !== specifier) | ||
path.unshift(newImport) | ||
} | ||
|
||
return | ||
} | ||
}) | ||
} | ||
|
||
module.exports = { | ||
handleExoticImport | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Animated as RNAnimated } from 'react-native' | ||
import { View } from './View' | ||
import { Text } from './Text' | ||
import { FlatList } from './FlatList' | ||
import { Image } from './Image' | ||
import { ScrollView } from './ScrollView' | ||
import { SectionList } from './SectionList' | ||
|
||
export const Animated = { | ||
...RNAnimated, | ||
View: RNAnimated.createAnimatedComponent(View), | ||
Text: RNAnimated.createAnimatedComponent(Text), | ||
FlatList: RNAnimated.createAnimatedComponent(FlatList), | ||
Image: RNAnimated.createAnimatedComponent(Image), | ||
ScrollView: RNAnimated.createAnimatedComponent(ScrollView), | ||
SectionList: RNAnimated.createAnimatedComponent(SectionList) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters