@@ -22,6 +22,8 @@ const styled = ({tagName, elementStyle, mountSheets}: StyledArgs) => {
2222 const dynamicStyle = getDynamicStyles ( elementStyle )
2323 const staticTagName = generateTagName ( tagName )
2424
25+ const availableDynamicTagNames = [ ]
26+
2527 return class StyledElement extends PureComponent {
2628 static tagName : string = tagName
2729 static style : ComponentStyleType = elementStyle
@@ -34,8 +36,8 @@ const styled = ({tagName, elementStyle, mountSheets}: StyledArgs) => {
3436
3537 constructor ( props : StyledElementPropsType ) {
3638 super ( props )
37- if ( ! this . dynamicTagName ) {
38- this . dynamicTagName = generateTagName ( tagName )
39+ if ( ! this . dynamicTagName && dynamicStyle ) {
40+ this . dynamicTagName = availableDynamicTagNames . pop ( ) || generateTagName ( tagName )
3941 }
4042 }
4143
@@ -46,15 +48,13 @@ const styled = ({tagName, elementStyle, mountSheets}: StyledArgs) => {
4648 this . staticSheet . addRule ( staticTagName , elementStyle )
4749 }
4850
49- if ( dynamicStyle && ! this . dynamicSheet . getRule ( this . dynamicTagName ) ) {
50- this . dynamicSheet
51- . detach ( )
52- . addRule ( this . dynamicTagName , dynamicStyle )
53- this . dynamicSheet
54- . update ( this . dynamicTagName , this . props )
55- . attach ( )
56- . link ( )
51+ if ( ! dynamicStyle ) return
52+
53+ if ( ! this . dynamicSheet . getRule ( this . dynamicTagName ) ) {
54+ this . dynamicSheet . addRule ( this . dynamicTagName , dynamicStyle )
5755 }
56+
57+ this . dynamicSheet . update ( this . dynamicTagName , this . props )
5858 }
5959
6060 componentWillReceiveProps ( nextProps : StyledElementPropsType ) {
@@ -63,17 +63,21 @@ const styled = ({tagName, elementStyle, mountSheets}: StyledArgs) => {
6363 }
6464 }
6565
66+ componentWillUnmount ( ) {
67+ availableDynamicTagNames . push ( this . dynamicTagName )
68+ }
69+
6670 render ( ) {
6771 if ( ! this . staticSheet ) return null
6872
6973 const { children, className, ...attrs } = this . props
7074
7175 const props = filterProps ( attrs )
72- const tagClass = composeClasses (
76+ const tagClass = composeClasses ( [
7377 this . staticSheet . classes [ staticTagName ] ,
7478 this . dynamicSheet . classes [ this . dynamicTagName ] ,
7579 className
76- )
80+ ] )
7781
7882 return createElement ( tagName , { ...props , className : tagClass } , children )
7983 }
0 commit comments