Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New branch #104

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ddbbde5
Add a support for attrs parameter while creating a styled component #68
zaxozhu Aug 5, 2019
fd14fda
Merge branch 'master' into feature/68-attrs-parameter
zaxozhu Aug 5, 2019
8e2cd0f
Add Readme #68
zaxozhu Aug 6, 2019
d2101e9
feat(): polymorphic as prop implementation
codebender828 Oct 24, 2019
e74c8b1
chore(docs): documenting test
codebender828 Oct 24, 2019
03db769
feat(docs): documented as prop implementation
codebender828 Oct 24, 2019
8292e4f
refactor(render): changed double pipe to asnyc await
codebender828 Oct 24, 2019
c506a87
feat(as): Added examples to documentation
codebender828 Oct 25, 2019
8cafbb4
chore: removed unused comments
codebender828 Nov 10, 2019
2ef0df4
docs: removed in code comments
codebender828 Nov 10, 2019
6c081d4
Fixed bug with styled-system to prevent undefined properties (issue #94)
Dec 31, 2019
f5c1745
Typo
Dec 31, 2019
d62ee9a
Merge branch 'master' of github.com:styled-components/vue-styled-comp…
codebender828 Jan 16, 2020
ca2a745
refactor(createStyledComponent): removed taget checks
codebender828 Jan 16, 2020
9968e52
docs(as prop): added docs for as prop anding
codebender828 Jan 16, 2020
1d77a10
docs: updated exampes and as prop function
codebender828 Jan 16, 2020
8575411
feat(as): added tests for as polymporphic prop
codebender828 Jan 16, 2020
ed965b6
docs(readme): updated readme docs with polymorphic as prop
codebender828 Jan 16, 2020
385e377
chore: cleanup code
codebender828 Jan 16, 2020
1def510
Bump eslint from 3.19.0 to 4.18.2
dependabot[bot] Nov 9, 2019
faf9a87
Merge branch 'master' of https://github.com/enjame/vue-styled-components
Kellaim Feb 27, 2020
b7719a0
resolve conflicts
Kellaim Feb 27, 2020
dadace2
merge
Kellaim Feb 27, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Remember to register `ThemeProvider` locally.

Add your `ThemeProvider` component:

```JSX
```vue
<theme-provider :theme="{
primary: 'palevioletred'
}">
Expand All @@ -190,6 +190,21 @@ And into your `Wrapper` component:
`;
```

### HTML Attributes

While creating a styled component you can pass an object that describes html attibutes of this component.

```JSX
import styled from 'vue-styled-components';

const StyledImage = styled('img', {}, { src: 'image.jpg', alt: 'Test image' })`
width: 50px;
height: 50px;
`;

export default StyledImage;
```

### Style component constructors as `router-link`

You can style also Vue component constructors as `router-link` from `vue-router` and other components
Expand Down Expand Up @@ -230,6 +245,26 @@ const TomatoButton = StyledButton.extend`
export default TomatoButton;
```

### Polymorphic `as` prop
If you want to keep all the styling you've applied to a component but just switch out what's being ultimately rendered (be it a different HTML tag or a different custom component), you can use the "as" prop to do this at runtime. Another powerful feature of the `as` prop is that it preserves styles if the lowest-wrapped component is a `StyledComponent`.

**Example**
In `Component.js`
```js
// Renders a div element by default.
const Component = styled('div', {})``
```
Using the `as` prop in another template/component would be as shown below.
```vue
<template>
<!-- Component will render a button instead of a div -->
<Component as="button" color="red">
Button
</Component>
</template>
```
This sort of thing is very useful in use cases like a navigation bar where some of the items should be links and some just buttons, but all be styled the same way.

### withComponent
Let's say you have a `button` and an `a` tag. You want them to share the exact same style. This is achievable with `.withComponent`.
```JSX
Expand Down
60 changes: 55 additions & 5 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ <h1>Basic Example</h1>
background: ${props => props.theme.tertiary};
`;

const W3 = styled.default(W2)`
&:hover {
background: ${props => props.theme.primary};
}
// Custom Wrapper
const W3 = styled.default(Wrapper, {
color: String,
bg: String
})`
color: ${props => props.color};
background-color: ${props => props.bg};
`


const StyledInput = styled.default.input`
display: block;
width: 100%;
Expand All @@ -106,19 +110,39 @@ <h1>Basic Example</h1>
margin: 1.5em 0;
background-color: ${props => props.theme.primary};
opacity: ${props => props.visible ? 1: 0};

&:focus {
outline: none;
border: solid 3px ${props => props.theme.primary};
}
`

const SuperBtn = styled.default(Btn, { visible: Boolean, big: Boolean })`
font-size: ${props => props.big ? 0.8 : 1}em;
`

const CustomBtn = styled.default(Btn, {
color: String,
bg: String
})`
color: ${props => props.color};
background-color: ${props => props.bg};

&:focus {
outline: none;
border: solid 3px ${props => props.color};
}
`

new Vue({
el: '#container',
data: {
text1: 'Hello World, this is my first styled component!',
text2: 'Hello World, this is my first styled component!',
animated1: false,
animated2: false
animated2: false,
element: 'div',
setElement: 'div'
},
template: `
<theme-provider :theme="{
Expand All @@ -136,18 +160,44 @@ <h1>Basic Example</h1>
<styled-input v-model="text2" />
<super-btn visible @click="animated2 = !animated2"> An extension of Styled Button </super-btn>
</w-2>
<w-3
bg="#c6f7e6"
>
<custom-title :visible="true">Change Rendered Elements with "as" prop.</custom-title>
<h3>Enter any HTML element and see the element rendered by the styled component change 👇🏽</h3>
<styled-input @keydown.enter="updateElement" placeholder="Enter an HTML element! Like 'button' or 'section'" v-model="element" />
<custom-btn
visible
:as="setElement"
role="button"
tabindex="0"
color="#42a786"
bg="papayawhip"
aria-label="Btn rendered as div"
@keydown.space="updateElement"
>
I am the "btn" component rendered as a "{{ setElement }}" using the \`as\` prop! 🎉
</custom-btn>
</w-3>
</theme-provider>`,
components: {
CustomTitle,
Wrapper,
W2,
W3,
H2,
Btn,
SuperBtn,
CustomBtn,
SuperCustomTitle,
StyledInput,
'theme-provider': styled.ThemeProvider,
},
methods: {
updateElement() {
this.setElement = this.element
}
}
});
</script>
</body>
Expand Down
Loading