Skip to content

Commit 08da79e

Browse files
authored
Merge pull request #62 from cristiandan/master
buttonBackground prop to customize the inner btn
2 parents 9291241 + 4e779a3 commit 08da79e

File tree

5 files changed

+114
-8
lines changed

5 files changed

+114
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Use:
4848
| color | [String, Object] | `#75C791` | If `String` - color of the button when checked <br>If `Object` - colors for the button when checked/uncheked <br>Example: `{checked: '#00FF00', unchecked: '#FF0000'}` |
4949
| cssColors | Boolean | false | If `true` - deactivates the setting of colors through inline styles in favor of using CSS styling |
5050
| labels | [Boolean, Object] | false | If `Boolean` - shows/hides default labels ("on" and "off") <br>If `Object` - sets custom labels for both states. <br>Example: `{checked: 'Foo', unchecked: 'Bar'}` |
51+
| switchColor | [String, Object] | `#BFCBD9` | If `String` - color or background property of the switch when checked <br>If `Object` - colors or background property for the switch when checked/uncheked <br>Example: `{checked: '#25EF02', unchecked: 'linear-gradient(red, yellow)'}` |
5152
| width | Number | 50 | Width of the button, default is 50 |
5253
| height | Number | 22 | Height of the button, default is 22 |
5354

demo/src/App.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
:color="{unchecked: '#FF6699'}"
3535
:labels="{unchecked: 'Disabled button'}"
3636
:disabled="true"/>
37+
38+
<toggle-button :value="true"
39+
:labels="{checked: 'Button', unchecked: 'Collor'}"
40+
:color="{checked: '#7DCE94', unchecked: '#82C7EB'}"
41+
:width="80"
42+
:switchColor="{checked: 'linear-gradient(red, yellow)', unchecked: '#F2C00B'}"/>
3743
</div>
3844
<div style="padding-top: 20px;">
3945
<toggle-button

dist/index.js

Lines changed: 33 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ssr.index.js

Lines changed: 33 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Button.vue

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ const constants = {
3232
labelUnchecked: 'off',
3333
width: 50,
3434
height: 22,
35-
margin: 3
35+
margin: 3,
36+
switchColor: '#fff'
3637
}
3738
3839
const contains = (object, title) => {
@@ -68,6 +69,14 @@ export default {
6869
: typeof value === 'string'
6970
}
7071
},
72+
switchColor: {
73+
type: [String, Object],
74+
validator (value) {
75+
return typeof value === 'object'
76+
? (value.checked || value.unchecked)
77+
: typeof value === 'string'
78+
}
79+
},
7180
cssColors: {
7281
type: Boolean,
7382
default: false
@@ -125,7 +134,8 @@ export default {
125134
transition: `transform ${this.speed}ms`,
126135
transform: this.toggled
127136
? `translate3d(${this.distance}, 3px, 0px)`
128-
: null
137+
: null,
138+
background: this.switchColor ? this.switchColorCurrent : undefined
129139
}
130140
},
131141
@@ -171,7 +181,36 @@ export default {
171181
return contains(this.labels, 'unchecked')
172182
? this.labels.unchecked
173183
: constants.labelUnchecked
184+
},
185+
186+
switchColorChecked () {
187+
let { switchColor } = this
188+
189+
return contains(switchColor, 'checked')
190+
? switchColor.checked
191+
: constants.switchColor
192+
},
193+
194+
switchColorUnchecked () {
195+
let { switchColor } = this
196+
197+
return contains(switchColor, 'unchecked')
198+
? switchColor.unchecked
199+
: constants.switchColor
200+
},
201+
202+
switchColorCurrent () {
203+
let { switchColor } = this
204+
205+
if (typeof switchColor !== 'object') {
206+
return switchColor || constants.switchColor
207+
}
208+
209+
return this.toggled
210+
? this.switchColorChecked
211+
: this.switchColorUnchecked
174212
}
213+
175214
},
176215
watch: {
177216
value (value) {

0 commit comments

Comments
 (0)