react-native-styled-sheet 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +28 -0
- package/LICENSE +21 -0
- package/package.json +42 -0
- package/src/index.d.ts +61 -0
- package/src/index.js +53 -0
- package/src/styled.js +26 -0
- package/src/utils/createStyledComponent.js +24 -0
- package/src/utils/cssToRN.js +29 -0
- package/src/utils/handleInheritance.js +34 -0
- package/src/utils/handleRegular.js +20 -0
package/.eslintrc.js
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module.exports = {
|
2
|
+
env: {
|
3
|
+
browser: true,
|
4
|
+
es2021: true,
|
5
|
+
node: true,
|
6
|
+
},
|
7
|
+
extends: [
|
8
|
+
'eslint:recommended',
|
9
|
+
'@react-native-community',
|
10
|
+
],
|
11
|
+
parserOptions: {
|
12
|
+
ecmaFeatures: {
|
13
|
+
jsx: true,
|
14
|
+
},
|
15
|
+
ecmaVersion: 12,
|
16
|
+
sourceType: 'module',
|
17
|
+
},
|
18
|
+
plugins: [
|
19
|
+
'react',
|
20
|
+
'react-native',
|
21
|
+
],
|
22
|
+
rules: {
|
23
|
+
'react-native/no-unused-styles': 'warn',
|
24
|
+
'react-native/split-platform-components': 'warn',
|
25
|
+
'react-native/no-inline-styles': 'warn',
|
26
|
+
'react-native/no-color-literals': 'warn',
|
27
|
+
},
|
28
|
+
};
|
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 eggmun98
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
{
|
2
|
+
"name": "react-native-styled-sheet",
|
3
|
+
"version": "0.1.11",
|
4
|
+
"description": "Styled-components like API for React Native using StyleSheet",
|
5
|
+
"main": "./src/index.js",
|
6
|
+
"author": "eggmun98",
|
7
|
+
"keywords": [
|
8
|
+
"react-native",
|
9
|
+
"styled-components",
|
10
|
+
"styled-sheet",
|
11
|
+
"styled-sheet-components",
|
12
|
+
"react-native-styled-sheet",
|
13
|
+
"react"
|
14
|
+
],
|
15
|
+
"license": "MIT",
|
16
|
+
"repository": {
|
17
|
+
"type": "git",
|
18
|
+
"url": "https://github.com/eggmun98/react-native-styled-sheet.git"
|
19
|
+
},
|
20
|
+
"packageManager": "pnpm@8.0.0",
|
21
|
+
"scripts": {
|
22
|
+
"lint": "eslint src/",
|
23
|
+
"lint:fix": "eslint src/ --fix",
|
24
|
+
"format": "prettier --write src/",
|
25
|
+
"dev": "echo 'Development ready'",
|
26
|
+
"release": "pnpm version patch && pnpm publish && git push && git push --tags"
|
27
|
+
},
|
28
|
+
"peerDependencies": {
|
29
|
+
"react": "*",
|
30
|
+
"react-native": "*"
|
31
|
+
},
|
32
|
+
"devDependencies": {
|
33
|
+
"@react-native-community/eslint-config": "^3.2.0",
|
34
|
+
"eslint": "^8.57.0",
|
35
|
+
"eslint-plugin-react": "^7.34.1",
|
36
|
+
"eslint-plugin-react-native": "^4.1.0",
|
37
|
+
"prettier": "^3.2.5"
|
38
|
+
},
|
39
|
+
"dependencies": {
|
40
|
+
"css-to-react-native": "^3.2.0"
|
41
|
+
}
|
42
|
+
}
|
package/src/index.d.ts
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import {
|
3
|
+
ViewProps,
|
4
|
+
TextProps,
|
5
|
+
TouchableOpacityProps,
|
6
|
+
TouchableHighlightProps,
|
7
|
+
TouchableWithoutFeedbackProps,
|
8
|
+
PressableProps,
|
9
|
+
ScrollViewProps,
|
10
|
+
ImageProps,
|
11
|
+
ImageBackgroundProps,
|
12
|
+
TextInputProps,
|
13
|
+
FlatListProps,
|
14
|
+
SectionListProps,
|
15
|
+
VirtualizedListProps,
|
16
|
+
SafeAreaViewProps,
|
17
|
+
KeyboardAvoidingViewProps,
|
18
|
+
RefreshControlProps,
|
19
|
+
} from 'react-native';
|
20
|
+
|
21
|
+
type TemplateStringsArray = ReadonlyArray<string>;
|
22
|
+
|
23
|
+
type StyledComponent<P = {}> = React.ComponentType<P> & {
|
24
|
+
__styledComponentMeta?: {
|
25
|
+
originalComponent: React.ComponentType<any>;
|
26
|
+
styles: any;
|
27
|
+
attrsProps: any;
|
28
|
+
};
|
29
|
+
};
|
30
|
+
|
31
|
+
interface StyledFunction<P = {}> {
|
32
|
+
(template: TemplateStringsArray, ...args: any[]): StyledComponent<P>;
|
33
|
+
attrs<AttrProps = {}>(
|
34
|
+
attrs: AttrProps
|
35
|
+
): (template: TemplateStringsArray, ...args: any[]) => StyledComponent<Omit<P, keyof AttrProps> & Partial<AttrProps>>;
|
36
|
+
}
|
37
|
+
|
38
|
+
interface StyledInterface {
|
39
|
+
<P = {}>(Component: React.ComponentType<P>): StyledFunction<P>;
|
40
|
+
<P = {}>(StyledComp: StyledComponent<P>): StyledFunction<P>;
|
41
|
+
|
42
|
+
View: StyledFunction<ViewProps>;
|
43
|
+
Text: StyledFunction<TextProps>;
|
44
|
+
TouchableOpacity: StyledFunction<TouchableOpacityProps>;
|
45
|
+
TouchableHighlight: StyledFunction<TouchableHighlightProps>;
|
46
|
+
TouchableWithoutFeedback: StyledFunction<TouchableWithoutFeedbackProps>;
|
47
|
+
Pressable: StyledFunction<PressableProps>;
|
48
|
+
ScrollView: StyledFunction<ScrollViewProps>;
|
49
|
+
Image: StyledFunction<ImageProps>;
|
50
|
+
ImageBackground: StyledFunction<ImageBackgroundProps>;
|
51
|
+
TextInput: StyledFunction<TextInputProps>;
|
52
|
+
FlatList: StyledFunction<FlatListProps<any>>;
|
53
|
+
SectionList: StyledFunction<SectionListProps<any, any>>;
|
54
|
+
VirtualizedList: StyledFunction<VirtualizedListProps<any>>;
|
55
|
+
SafeAreaView: StyledFunction<SafeAreaViewProps>;
|
56
|
+
KeyboardAvoidingView: StyledFunction<KeyboardAvoidingViewProps>;
|
57
|
+
RefreshControl: StyledFunction<RefreshControlProps>;
|
58
|
+
}
|
59
|
+
|
60
|
+
declare const styled: StyledInterface;
|
61
|
+
export default styled;
|
package/src/index.js
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import {
|
3
|
+
View,
|
4
|
+
Text,
|
5
|
+
TouchableOpacity,
|
6
|
+
TouchableHighlight,
|
7
|
+
TouchableWithoutFeedback,
|
8
|
+
Pressable,
|
9
|
+
ScrollView,
|
10
|
+
Image,
|
11
|
+
ImageBackground,
|
12
|
+
TextInput,
|
13
|
+
FlatList,
|
14
|
+
SectionList,
|
15
|
+
VirtualizedList,
|
16
|
+
SafeAreaView,
|
17
|
+
KeyboardAvoidingView,
|
18
|
+
RefreshControl,
|
19
|
+
} from 'react-native';
|
20
|
+
import { createStyledComponent } from './utils/createStyledComponent';
|
21
|
+
|
22
|
+
function styled(Component) {
|
23
|
+
const createStyled = function (strings) {
|
24
|
+
return createStyledComponent(Component, strings);
|
25
|
+
};
|
26
|
+
|
27
|
+
createStyled.attrs = function(attrsProps) {
|
28
|
+
return function(strings) {
|
29
|
+
return createStyledComponent(Component, strings, attrsProps);
|
30
|
+
};
|
31
|
+
};
|
32
|
+
|
33
|
+
return createStyled;
|
34
|
+
}
|
35
|
+
|
36
|
+
styled.View = styled(View);
|
37
|
+
styled.Text = styled(Text);
|
38
|
+
styled.TouchableOpacity = styled(TouchableOpacity);
|
39
|
+
styled.TouchableHighlight = styled(TouchableHighlight);
|
40
|
+
styled.TouchableWithoutFeedback = styled(TouchableWithoutFeedback);
|
41
|
+
styled.Pressable = styled(Pressable);
|
42
|
+
styled.ScrollView = styled(ScrollView);
|
43
|
+
styled.Image = styled(Image);
|
44
|
+
styled.ImageBackground = styled(ImageBackground);
|
45
|
+
styled.TextInput = styled(TextInput);
|
46
|
+
styled.FlatList = styled(FlatList);
|
47
|
+
styled.SectionList = styled(SectionList);
|
48
|
+
styled.VirtualizedList = styled(VirtualizedList);
|
49
|
+
styled.SafeAreaView = styled(SafeAreaView);
|
50
|
+
styled.KeyboardAvoidingView = styled(KeyboardAvoidingView);
|
51
|
+
styled.RefreshControl = styled(RefreshControl);
|
52
|
+
|
53
|
+
export default styled;
|
package/src/styled.js
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
import { handleRegular } from './utils/handleRegular';
|
2
|
+
import { handleInheritance } from './utils/handleInheritance';
|
3
|
+
|
4
|
+
/**
|
5
|
+
* styled 함수 - React Native 컴포넌트를 styled-components 문법으로 스타일링
|
6
|
+
*
|
7
|
+
* 기본 사용법:
|
8
|
+
* const StyledView = styled.View`background-color: red;`;
|
9
|
+
*
|
10
|
+
* attrs 사용법:
|
11
|
+
* const StyledButton = styled.TouchableOpacity.attrs({ activeOpacity: 0.8 })`...`;
|
12
|
+
*
|
13
|
+
* 상속 사용법:
|
14
|
+
* const BaseButton = styled.TouchableOpacity`padding: 10px;`;
|
15
|
+
* const BlueButton = styled(BaseButton)`background-color: blue;`;
|
16
|
+
*
|
17
|
+
* @param {React.ComponentType} Component - React Native 컴포넌트 또는 styled component
|
18
|
+
* @returns {Function} 템플릿 리터럴을 받는 고차 함수
|
19
|
+
*/
|
20
|
+
export function styled(Component) {
|
21
|
+
const isInheritable = Component && Component.__styledComponentMeta;
|
22
|
+
|
23
|
+
return isInheritable
|
24
|
+
? handleInheritance(Component)
|
25
|
+
: handleRegular(Component);
|
26
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { cssToRN } from './cssToRN';
|
3
|
+
|
4
|
+
/**
|
5
|
+
* styled component를 생성하는 핵심 함수
|
6
|
+
* @param {React.ComponentType} Component - 래핑할 React Native 컴포넌트
|
7
|
+
* @param {string[]} strings - 템플릿 리터럴의 문자열 배열
|
8
|
+
* @param {object} attrsProps - attrs()로 전달된 기본 props (선택적)
|
9
|
+
* @returns {React.ComponentType} 스타일이 적용된 React 컴포넌트
|
10
|
+
*/
|
11
|
+
export const createStyledComponent = (Component, strings, attrsProps) => {
|
12
|
+
return function StyledComponent(props) {
|
13
|
+
const cssString = strings.join('');
|
14
|
+
|
15
|
+
const style = cssToRN(cssString);
|
16
|
+
|
17
|
+
const finalProps = attrsProps ? { ...attrsProps, ...props } : props;
|
18
|
+
|
19
|
+
return React.createElement(Component, {
|
20
|
+
...finalProps,
|
21
|
+
style: [style, finalProps.style],
|
22
|
+
});
|
23
|
+
};
|
24
|
+
};
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import transformDeclPairs from 'css-to-react-native';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* CSS 문자열을 React Native 스타일 객체로 변환하는 함수
|
5
|
+
* @param {string} cssString - 변환할 CSS 문자열 (예: "background-color: red; padding: 20px;")
|
6
|
+
* @returns {object} React Native 스타일 객체 (예: { backgroundColor: 'red', paddingTop: 20, ... })
|
7
|
+
*/
|
8
|
+
export const cssToRN = (cssString) => {
|
9
|
+
try {
|
10
|
+
if (!cssString || !cssString.trim()) {
|
11
|
+
return {};
|
12
|
+
}
|
13
|
+
|
14
|
+
const declPairs = cssString
|
15
|
+
.split(';')
|
16
|
+
.map((line) => line.trim())
|
17
|
+
.filter(Boolean)
|
18
|
+
.map((line) => {
|
19
|
+
const [prop, ...value] = line.split(':');
|
20
|
+
return [prop.trim(), value.join(':').trim()];
|
21
|
+
})
|
22
|
+
.filter(([prop, value]) => prop && value);
|
23
|
+
|
24
|
+
return transformDeclPairs(declPairs);
|
25
|
+
} catch (error) {
|
26
|
+
console.warn('Error transforming CSS to React Native style:', error);
|
27
|
+
return {};
|
28
|
+
}
|
29
|
+
};
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { createStyledComponent } from './createStyledComponent';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* styled component 상속 처리
|
5
|
+
* @param {React.ComponentType} StyledComponent - 상속할 styled component
|
6
|
+
* @returns {Function} 상속된 styled 함수 (attrs 메서드 포함)
|
7
|
+
*/
|
8
|
+
export const handleInheritance = (StyledComponent) => {
|
9
|
+
const meta = StyledComponent.__styledComponentMeta;
|
10
|
+
|
11
|
+
const createInheritedStyled = function (strings) {
|
12
|
+
return createStyledComponent(
|
13
|
+
meta.originalComponent, // 원본 컴포넌트 사용
|
14
|
+
strings,
|
15
|
+
meta.attrsProps, // 상속받은 attrs
|
16
|
+
meta.styles // 상속받은 스타일
|
17
|
+
);
|
18
|
+
};
|
19
|
+
|
20
|
+
createInheritedStyled.attrs = function(newAttrsProps) {
|
21
|
+
return function(strings) {
|
22
|
+
// attrs 병합: 기존 attrs + 새로운 attrs (새로운 것이 우선)
|
23
|
+
const mergedAttrs = { ...meta.attrsProps, ...newAttrsProps };
|
24
|
+
return createStyledComponent(
|
25
|
+
meta.originalComponent,
|
26
|
+
strings,
|
27
|
+
mergedAttrs,
|
28
|
+
meta.styles
|
29
|
+
);
|
30
|
+
};
|
31
|
+
};
|
32
|
+
|
33
|
+
return createInheritedStyled;
|
34
|
+
};
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { createStyledComponent } from './createStyledComponent';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* 일반 React Native 컴포넌트 스타일링 처리
|
5
|
+
* @param {React.ComponentType} Component - React Native 컴포넌트
|
6
|
+
* @returns {Function} styled 함수 (attrs 메서드 포함)
|
7
|
+
*/
|
8
|
+
export const handleRegular = (Component) => {
|
9
|
+
const createStyled = function (strings) {
|
10
|
+
return createStyledComponent(Component, strings);
|
11
|
+
};
|
12
|
+
|
13
|
+
createStyled.attrs = function(attrsProps) {
|
14
|
+
return function(strings) {
|
15
|
+
return createStyledComponent(Component, strings, attrsProps);
|
16
|
+
};
|
17
|
+
};
|
18
|
+
|
19
|
+
return createStyled;
|
20
|
+
};
|