Comments (9)
hi @RickEyre
Thanks for this suggestion!
But I see some problems with this feature, and the most meaningful problem for me is possible performance downgrade for this case. So let me explain how styled-jss
works now.
When we mount styled component created with styled(tag)(styles)
for the first time ,jss
parses styles
object and creates the Rule
from it, that will be used in CSSOM. So in this processjss
also handles function values, applies different jss-plugins
and so on. And it's quite expensive at all. But we've got many optimizations later, when we reuse or update this component, like:
styled-jss
reuse this calculations, and it does not parsestyles
again- function values are really fast on update
That's why styled-jss
is one of the fastest cssinjs
solutions on rerender without inline-styles. And of course, we're trying to improve our first-mount time
too.
Let's take a look at this example:
const cases = {
one: {color: 'red'},
two: {color: 'glue'}
}
const StyledComponent = styled('button')({
color: props => cases[props.type].color
})
// ...
export default (props) => (
<div>
<StyledComponent type="one">button 1</StyledComponent>
<StyledComponent type="two">button 2</StyledComponent>
<StyledComponent type={props.something ? 'one' : 'two'}>button</StyledComponent>
</div>
)
So on each rerender and mount we will just recalculate the dynamic value
of created StyledComponent Rule
with function color: props => cases[props.type]
for each instance, and it will be cheap and fast.
And now let's check an example with suggested behavior:
const CaseOne = {
color: red
};
const CaseTwo = {
color: blue
};
const MyStyledComponent = styled('button')(
props => props.caseOne ? CaseOne : CaseTwo
);
To make this thing work, we need to recalculate the whole styles object for each render and mount, that means that we should parse styles object and fully update or create the Rule every time again. It seems that it may cause some performance problems.
We will try to improve our first-mount
time and make styled-jss
faster at all, but nowadays, unfortunately, I don't see the common effective solution for such behavior.
Probably we may resolve this problem with shallow styles equals and smart Rule
updates by diff, but it's necessary to investigate.
Maybe you have some suggestions here?
from styled-jss.
Currently, I made this via recompose branch
, because we can create static subcomponents, which represents our cases
import { branch, renderComponent } from 'recompose'
const RadioBase = styled('input')({
content: 'some our style'
})
const RadioActive = styled(RadioBase)({
cursor: 'pointer',
})
const RadioDisabled = styled(RadioBase)({
cursor: 'not-allowed',
})
const Radio = branch(
({ disabled }) => !!disabled,
renderComponent(RadioDisabled),
// renderComponent means that we just render RadioDisabled
)(RadioActive)
I think that its possible to declare one united style (as in Styled
) with cases referenced by choose functions.
But I don't want to mess with completely untyped composes: '$baseButton'
, prefer to returns components directly, so it can look something like that
const Base = styled('input')({
content: 'some our style'
})
const Result = styledSwitch({
active: {
cursor: 'pointer',
},
disabled: {
cursor: 'not-allowed',
},
}, (props, cases) => props.active
? cases.active // Note that we switch between components, not raw props
: cases.disabled
)( Base )
from styled-jss.
Oops, noted that it still doesn't solve issue yet, so I hope at least it yields some good thoughts
from styled-jss.
@lttb does glamorous
also do recalculation for each render?
from styled-jss.
function rules have been added to the recent jss version, so this should work out of the box or easy to add now
from styled-jss.
@lttb do you plan to add it?
from styled-jss.
@goodmind yeah, I'm almost done with it here #35
There are some updates to API, like Theming and Composable Styles
from styled-jss.
Resolved in #35
from styled-jss.
Released in 2.2.0.
You can check the example here: https://codesandbox.io/s/y0162p38lv
from styled-jss.
Related Issues (20)
- Overriding component style HOT 2
- Border property HOT 2
- Support Observables from JSS9
- What about keyframes animation? HOT 7
- Add plugins without re-exporting styled HOT 6
- react-jss and styled-jss in the same app HOT 3
- Remove isObservable checks
- SSR Support for styled-jss
- SSR Support HOT 1
- Media queries don't work when using theming function HOT 9
- Bug: Nested composition causes invalid tag to be rendered HOT 1
- Support getting the ref of the wrapped component
- Add a way to filter props when extending other components
- Type Definitions HOT 1
- Mixing style objects and functions messes up specificity resolution HOT 1
- Composing function styles causes dynamic styles to not be used
- @global is not working HOT 4
- Component Name as the class name for easier debugging?
- How to install styled-jss when used in a library HOT 1
- Dynamic import styled-jss
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from styled-jss.