vitalets/react-native-extended-stylesheet là một trong những repo tập trung giao diện mà TopGit theo dõi, hiện có 2.9k sao, viết chủ yếu bằng JavaScript. Extended StyleSheets for React Native
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
This library is deprecated and will no longer receive any updates or support.
Please consider migrating to alternative solutions. See #154 for details.
Drop-in replacement of React Native StyleSheet with media-queries, variables, dynamic themes,
relative units, percents, math operations, scaling and other styling stuff.
Demo
Installation
Usage
Features
global variables
local variables
theming
media queries
math operations
rem units
percents
scaling
underscored styles
pseudo classes (:nth-child)
value as a function
caching
outline for debug
hot module reload
API
.create()
.build()
.value()
.child()
.subscribe()
.unsubscribe()
Caveats
FAQ
Changelog
Feedback
License
Demo
Use this Expo snack to play with Extended StyleSheets
right in the browser or in Expo app.
Installation
npm i react-native-extended-stylesheet --save
Usage
Define styles using EStyleSheet.create() instead of StyleSheet.create():
/* component.js */
import EStyleSheet from 'react-native-extended-stylesheet';
// define extended styles
const styles = EStyleSheet.create({
column: {
width: '80%' // 80% of screen width
},
text: {
color: '$textColor', // global variable $textColor
fontSize: '1.5rem' // relative REM unit
},
'@media (min-width: 350) and (max-width: 500)': { // media queries
text: {
fontSize: '2rem',
}
}
});
// use styles as usual
class MyComponent extends React.Component {
render() {
return (
<View style={styles.column}>
<Text style={styles.text}>Hello</Text>
</View>
);
}
}
In app entry point call EStyleSheet.build() to actually calculate styles:
/* app.js */
import EStyleSheet from 'react-native-extended-stylesheet';
EStyleSheet.build({ // always call EStyleSheet.build() even if you don't use global variables!
$textColor: '#0275d8'
});
[top]
Features
Global variables
Global variables are passed to EStyleSheet.build() and available in all stylesheets.
// app entry: set global variables and calc styles
EStyleSheet.build({
$textColor: '#0275d8'
});
// component: use global variables
const styles = EStyleSheet.create({
text: {
color: '$textColor'
}
});
// global variable as inline style or as props to components
<View style = {{
backgroundColor: EStyleSheet.value('$textColor')
}}>
...
</View>
[top]
Local variables
Local variables can be defined directly in sylesheet and have priority over global variables.
To define local variable just start it with $:
The caveat is that all components loss their state.
In the future it may be possible with forceDeepUpdate() method (see facebook/react#7759).
The approach is open for discusison, feel free to share your ideas in #22,
#47.
You can check out full theming code in examples/theming or in Expo snack.
[top]
Media queries
Media queries allows to have different styles for different screens, platform, direction and orientation.
They are supported as properties with @media prefix (thanks for idea to @grabbou,
#5).
Media queries can operate with the following values:
You can check out full example code in examples/media-queries or in Expo snack.
[top]
Math operations
Any value can contain one of following math operations: *, /, +, -. Operands can be numbers, variables and percents.
For example, to render circle you may create style:
Similar to CSS3 rem unit it allows to define any integer value as relative to the root element. In our case root value is special rem global variable that can be set in EStyleSheet.build(). It makes easy to scale app depending on screen size and other conditions. Default rem is 16.
You can check out full example code in examples/rem or in Expo snack.
[top]
Percents
Percent values are supported natively since React Native 0.43.
EStyleSheet passes them through to original StyleSheet except cases, when you use calculations with percents,
e.g. "100% - 20". Percents are calculated relative to screen width/height on application launch.
Percents in nested components
If you need sub-component with percent operations relative to parent component - you can achieve that with variables.
For example, to render 2 sub-columns with 30%/70% width of parent column:
To cache calculated styles please have a look on caching section.
[top]
Underscored styles
Original react-native stylesheets are calculated to integer numbers and original values are unavailable.
But sometimes they are needed. Let's take an example:
You want to render text and icon with the same size and color.
You can take this awesome icon library
and see that <Icon> component has size and color props.
It would be convenient to define style for text and keep icon's size/color in sync.
Extended stylesheet supports 4 pseudo classes: :first-child, :nth-child-even, :nth-child-odd, :last-child. As well as in traditional CSS it allows to apply special styling for first/last items or render stripped rows.
To get style for appropriate index you should use EStyleSheet.child() method.
It's signature: EStyleSheet.child(stylesObj, styleName, index, count).
For the deepest customization you can specify any value as a function that will be executed on EStyleSheet build.
For example, you may darken or lighten color of variable via npm color package:
import Color from 'color';
import EStyleSheet from 'react-native-extended-stylesheet';
const styles = EStyleSheet.create({
button: {
backgroundColor: () => Color('green').darken(0.1).hexString() // <-- value as a function
}
});
render() {
return (
<TouchableHighlight style={styles.button}>
...
</TouchableHighlight>
);
}
The common pattern is to use EStyleSheet.value() inside the function to get access to global variables:
If you use dynamic styles depending on runtime prop or you are making reusable component with dynamic styling
you may need stylesheet creation in every render() call. Let's take example from scaling section:
To avoid creating styles on every render you can use lodash.memoize:
store result for particular parameters and returns it from cache when called with the same parameters.
Updated example:
Hot module reload (HMR)
allows you to change code and see live updates without loosing app state. It is very handy for tuning styles.
EStyleSheet supports HMR with the following options:
When you change style of component - the component is updated by HMR automatically without any effort from your side.
When you change global variable or theme - you should use HMR API
to force style re-calculation:
/**
* Calculates all stylesheets
*
* @param {Object} [globalVars] global variables for all stylesheets
*/
build (globalVars) {...}
[top]
.value()
/**
* Calculates particular expression.
*
* @param {*} value
* @param {String} [prop] property for which value is calculated. For example, to calculate percent values
* the function should know is it 'width' or 'height' to use proper reference value.
* @returns {*} calculated result
*/
value (value, prop) {...}
Please note that in most cases EStyleSheet.value() should be used inside function, not directly:
const styles = EStyleSheet.create({
button1: {
width: () => EStyleSheet.value('$contentWidth') + 10 // <-- Correct!
},
button2: {
width: EStyleSheet.value('$contentWidth') + 10 // <-- Incorrect. Because EStyleSheet.build() may occur later and $contentWidth will be undefined at this moment.
}
});
[top]
.child()
/**
* Returns styles with pseudo classes :first-child, :nth-child-even, :last-child according to index and count
*
* @param {Object} stylesheet
* @param {String} styleName
* @param {Number} index index of item for style
* @param {Number} count total count of items
* @returns {Object|Array} styles
*/
child (styles, styleName, index, count) {...}
[top]
.subscribe()
/**
* Subscribe to event. Currently only 'build' event is supported.
*
* @param {String} event
* @param {Function} listener
*/
subscribe (event, listener) {...}
This method is useful when you want to pre-render some component on init.
As extended style is calculated after call of EStyleSheet.build(),
it is not available instantly after creation so you should wrap pre-render
info listener to build event:
const styles = EStyleSheet.create({
button: {
width: '80%',
}
});
// this will NOT work as styles.button is not calculated yet
let Button = <View style={styles.button}></View>;
// but this will work
let Button;
EStyleSheet.subscribe('build', () => {
Button = <View style={styles.button}></View>;
});
[top]
.unsubscribe()
/**
* Unsubscribe from event. Currently only 'build' event is supported.
*
* @param {String} event
* @param {Function} listener
*/
unsubscribe (event, listener) {...}
Unsubscribe from event.
[top]
Caveats
Dynamic theme change is possible only with loosing components local state
When theme styles are re-calculated - all components should be re-rendered.
Currently it can be done via re-mounting components tree, please see #47.
Note: it is not issue if you are using state container like Redux
and can easily re-render app in the same state
Dynamic orientation change is not supported
Please see #9 for more details.
Old RN versions (< 0.43) can crash the app with percent values
RN >= 0.43 supports percent values natively (#32) and EStyleSheet since 0.5.0 just proxy percent values to RN as is (#77) to keep things simple.
Older RN versions (< 0.43) can't process percents and EStyleSheet process such values.
So if you are using RN < 0.43, you should stick to [email protected].
FAQ
I'm getting error: "Unresolved variable: ..."
Ensure that you call EStyleSheet.build() in entry point of your app.
Ensure that $variable name without typos.
Ensure that you are not using EStyleSheet.value() before the styles are built. See #50 for details.
Changelog
Please see CHANGELOG.md
Feedback
If you have any ideas or something goes wrong feel free to
open new issue.
License
MIT @ Vitaliy Potapov
[top]
* * * If you love :heart: JavaScript and would like to track new trending repositories,
have a look on vitalets/github-trending-repos.
vitalets/react-native-extended-stylesheet thuộc nhóm Frontend trên TopGit, cùng 7 topic GitHub. Trang Trending và Topics liệt kê các repo cùng số sao và cùng ngôn ngữ để so sánh.
Đọc thêm về vitalets/react-native-extended-stylesheet ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/vitalets/react-native-extended-stylesheet là nguồn chính thức.
vitalets/react-native-extended-stylesheet có bao nhiêu sao?
vitalets/react-native-extended-stylesheet có 2.9k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/vitalets/react-native-extended-stylesheet. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
vitalets/react-native-extended-stylesheet có phải mã nguồn mở không?
Có — vitalets/react-native-extended-stylesheet phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/vitalets/react-native-extended-stylesheet.
vitalets/react-native-extended-stylesheet còn đang phát triển không?
Commit gần nhất trên vitalets/react-native-extended-stylesheet là 1.5 năm trước (theo timestamp GitHub). Repo có 128 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
vitalets/react-native-extended-stylesheet dùng license gì?
vitalets/react-native-extended-stylesheet phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
vitalets/react-native-extended-stylesheet là gì?
vitalets/react-native-extended-stylesheet (vitalets/react-native-extended-stylesheet) là dự án JavaScript trên GitHub. Theo mô tả gốc: Extended StyleSheets for React Native
vitalets/react-native-extended-stylesheet viết bằng ngôn ngữ gì?
vitalets/react-native-extended-stylesheet chủ yếu viết bằng JavaScript. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
react-native-extended-stylesheet có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về react-native-extended-stylesheet.