Skip to content

Commit 137de64

Browse files
committed
[+] Fixed StyledComponent.extend when it's used with props
1 parent 8dbfd72 commit 137de64

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

example/index.html

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,31 @@ <h1>Basic Example</h1>
2929
}
3030
`;
3131

32+
const VueComponent = {
33+
functional: true,
34+
render: function (createElement, context) {
35+
// Transparently pass any attributes, event listeners, children, etc.
36+
return createElement('button', context.data, context.children)
37+
}
38+
}
39+
40+
const StyledVueComponent = styled.default(VueComponent)``
41+
3242
// Create a <CustomTitle> vue component that renders an <h1> which is
3343
// centered, palevioletred and sized at 1.5em
34-
const CustomTitle = styled.default.h1`
44+
const CustomTitle = styled.default('h1', { animate: Boolean })`
3545
font-size: 2em;
3646
text-align: center;
3747
color: ${props => props.theme.primary};
38-
animation: ${animation} 2s linear infinite;
3948
4049
@supports (-webkit-text-stroke: 1px) {
4150
-webkit-text-stroke: 2px ${props => props.theme.primary};
4251
color: transparent
4352
}
53+
54+
${props => props.animate &&
55+
`animation: ${animation} 2s linear infinite;`
56+
}
4457
`;
4558

4659
const H2 = CustomTitle.withComponent('h2')
@@ -52,8 +65,8 @@ <h1>Basic Example</h1>
5265
background: ${props => props.theme.secondary};
5366
`;
5467

55-
const W2 = styled.default(Wrapper)`
56-
color: red;
68+
const W2 = Wrapper.extend`
69+
background: ${props => props.theme.tertiary};
5770
`;
5871

5972
const W3 = styled.default(W2)`
@@ -62,6 +75,13 @@ <h1>Basic Example</h1>
6275
}
6376
`
6477

78+
const StyledInput = styled.default.input`
79+
display: block;
80+
width: 100%;
81+
height: 60px;
82+
border: solid 1px ${props => props.theme.primary};
83+
`
84+
6585
const Btn = styled.default('button', { visible: Boolean })`
6686
background-color: red;
6787
opacity: ${props => props.visible ? 1: 0};
@@ -73,21 +93,42 @@ <h1>Basic Example</h1>
7393

7494
new Vue({
7595
el: '#container',
96+
data: {
97+
text: 'Hello World, this is my first styled component!'
98+
},
99+
methods: {
100+
input(e) {
101+
console.log(e.target.value)
102+
this.text += e.target.value
103+
}
104+
},
76105
template: `
77106
<theme-provider :theme="{
78107
primary: 'palevioletred',
79-
secondary: 'papayawhip'
108+
secondary: 'papayawhip',
109+
tertiary: 'goldenrod'
80110
}">
81-
<wrapper>
82-
<custom-title> Hello World, this is my first styled component! </custom-title>
111+
<wrapper color="salmon">
112+
<custom-title> {{text}} </custom-title>
113+
<styled-input
114+
v-bind:value="text"
115+
v-on:input="text = $event.target.value"
116+
/>
117+
<input
118+
v-bind:value="text"
119+
v-on:input="text = $event.target.value"
120+
/>
83121
<btn visible> Button </btn>
122+
<styled-vue-component>ciao</styled-vue-component>
84123
</wrapper>
85124
</theme-provider>`,
86125
components: {
87126
'custom-title': H2,
88-
wrapper: W3,
127+
wrapper: W2,
89128
btn: SuperBtn,
90-
'theme-provider': styled.ThemeProvider
129+
StyledVueComponent,
130+
'styled-input': StyledInput,
131+
'theme-provider': styled.ThemeProvider,
91132
},
92133
});
93134
</script>

src/models/StyledComponent.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import css from '../constructors/css'
2+
13
export default (ComponentStyle) => {
24
const createStyledComponent = (target, rules, props) => {
35
const prevProps = target && typeof target !== 'string'
@@ -27,8 +29,6 @@ export default (ComponentStyle) => {
2729
}
2830
}
2931

30-
console.log()
31-
3232
return createElement(
3333
target,
3434
{
@@ -57,8 +57,9 @@ export default (ComponentStyle) => {
5757
return this.$theme()
5858
}
5959
},
60-
extend (extendedRules) {
61-
return createStyledComponent(target, rules.slice().concat(extendedRules), props)
60+
extend (cssRules, ...interpolations) {
61+
const extendedRules = css(cssRules, ...interpolations)
62+
return createStyledComponent(target, rules.concat(extendedRules), props)
6263
},
6364
withComponent (newTarget) {
6465
return createStyledComponent(newTarget, rules, props)

src/utils/isValidElementType.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default function isValidElementType (tag) {
88
return domElements.indexOf(tag) !== -1
99
}
1010
if (typeof tag === 'object') {
11-
return !!tag.template || !!tag.withComponent
11+
return !!tag.template || !!tag.withComponent || !!tag.functional
1212
}
1313
return true
1414
}

0 commit comments

Comments
 (0)