Skip to content
This repository was archived by the owner on Sep 11, 2021. It is now read-only.

Commit b0fd184

Browse files
committed
Support specifying default icon/iconColor at element root
1 parent f31a4ab commit b0fd184

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

src/defaultOptions.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ module.exports = {
8686
iconColor: defaultTheme.colors.gray[500],
8787
backgroundPosition: `right ${defaultTheme.spacing[2]} center`,
8888
backgroundSize: `1.5em 1.5em`,
89+
icon: iconColor => `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="${iconColor}"><path d="M15.3 9.3a1 1 0 0 1 1.4 1.4l-4 4a1 1 0 0 1-1.4 0l-4-4a1 1 0 0 1 1.4-1.4l3.3 3.29 3.3-3.3z"/></svg>`,
8990
'&:focus': {
9091
outline: 'none',
9192
boxShadow: defaultTheme.boxShadow.outline,
@@ -110,18 +111,20 @@ module.exports = {
110111
flexShrink: 0,
111112
height: '1em',
112113
width: '1em',
114+
color: defaultTheme.colors.blue[500],
115+
backgroundColor: defaultTheme.colors.white,
113116
borderColor: defaultTheme.borderColor.default,
114117
borderWidth: defaultTheme.borderWidth.default,
115118
borderRadius: defaultTheme.borderRadius.default,
116-
backgroundColor: defaultTheme.colors.white,
119+
icon: iconColor => `<svg viewBox="0 0 16 16" fill="${iconColor}" xmlns="http://www.w3.org/2000/svg"><path d="M5.707 7.293a1 1 0 0 0-1.414 1.414l2 2a1 1 0 0 0 1.414 0l4-4a1 1 0 0 0-1.414-1.414L7 8.586 5.707 7.293z"/></svg>`,
117120
'&:focus': {
118121
outline: 'none',
119122
boxShadow: defaultTheme.boxShadow.outline,
120123
borderColor: defaultTheme.colors.blue[400],
121124
},
122125
'&:checked': {
123-
backgroundColor: defaultTheme.colors.blue[500],
124-
borderColor: defaultTheme.colors.blue[500],
126+
borderColor: 'transparent',
127+
backgroundColor: 'currentColor',
125128
backgroundSize: '100% 100%',
126129
backgroundPosition: 'center',
127130
backgroundRepeat: 'no-repeat',
@@ -147,17 +150,19 @@ module.exports = {
147150
borderRadius: '100%',
148151
height: '1em',
149152
width: '1em',
153+
color: defaultTheme.colors.blue[500],
154+
backgroundColor: defaultTheme.colors.white,
150155
borderColor: defaultTheme.borderColor.default,
151156
borderWidth: defaultTheme.borderWidth.default,
152-
backgroundColor: defaultTheme.colors.white,
157+
icon: iconColor => `<svg viewBox="0 0 16 16" fill="${iconColor}" xmlns="http://www.w3.org/2000/svg"><circle cx="8" cy="8" r="3"/></svg>`,
153158
'&:focus': {
154159
outline: 'none',
155160
boxShadow: defaultTheme.boxShadow.outline,
156161
borderColor: defaultTheme.colors.blue[400],
157162
},
158163
'&:checked': {
159-
backgroundColor: defaultTheme.colors.blue[500],
160-
borderColor: defaultTheme.colors.blue[500],
164+
borderColor: 'transparent',
165+
backgroundColor: 'currentColor',
161166
backgroundSize: '100% 100%',
162167
backgroundPosition: 'center',
163168
backgroundRepeat: 'no-repeat',

src/index.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function resolveOptions(userOptions) {
4040
}, fromPairs(map(userOptions, (value, key) => [key, flattenOptions(value)])))
4141
}
4242

43-
function replaceIconDeclarations(component, defaultIcon) {
43+
function replaceIconDeclarations(component, replace) {
4444
return traverse(component).map(function (value) {
4545
if (!isPlainObject(value)) {
4646
return
@@ -50,7 +50,7 @@ function replaceIconDeclarations(component, defaultIcon) {
5050
const { iconColor, icon, ...rest } = value
5151
this.update({
5252
...rest,
53-
backgroundImage: `url("${svgToDataUri(isUndefined(icon) ? defaultIcon(iconColor) : (isFunction(icon) ? icon(iconColor) : icon))}")`
53+
...replace({ icon, iconColor }),
5454
})
5555
}
5656
})
@@ -97,13 +97,15 @@ module.exports = function ({ addUtilities, addComponents, theme, postcss }) {
9797
},
9898
},
9999
}, options)
100-
}, options.icon || (iconColor => {
101-
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="${iconColor}"><path d="M15.3 9.3a1 1 0 0 1 1.4 1.4l-4 4a1 1 0 0 1-1.4 0l-4-4a1 1 0 0 1 1.4-1.4l3.3 3.29 3.3-3.3z"/></svg>`
102-
})))
100+
}, ({ icon = options.icon, iconColor = options.iconColor }) => {
101+
return {
102+
backgroundImage: `url("${svgToDataUri(isFunction(icon) ? icon(iconColor) : icon)}")`
103+
}
104+
}))
103105
}
104106

105-
function addCheckbox({ icon, ...options}, modifier = '') {
106-
if (isEmpty(options) && isUndefined(icon)) {
107+
function addCheckbox(options, modifier = '') {
108+
if (isEmpty(options)) {
107109
return
108110
}
109111

@@ -117,13 +119,17 @@ module.exports = function ({ addUtilities, addComponents, theme, postcss }) {
117119
},
118120
},
119121
}, options)
120-
}, icon || (iconColor => {
121-
return `<svg viewBox="0 0 16 16" fill="${iconColor}" xmlns="http://www.w3.org/2000/svg"><path d="M5.707 7.293a1 1 0 0 0-1.414 1.414l2 2a1 1 0 0 0 1.414 0l4-4a1 1 0 0 0-1.414-1.414L7 8.586 5.707 7.293z"/></svg>`
122-
})))
122+
}, ({ icon = options.icon, iconColor = options.iconColor }) => {
123+
return {
124+
'&:checked': {
125+
backgroundImage: `url("${svgToDataUri(isFunction(icon) ? icon(iconColor) : icon)}")`
126+
}
127+
}
128+
}))
123129
}
124130

125-
function addRadio({ icon, ...options }, modifier = '') {
126-
if (isEmpty(options) && isUndefined(icon)) {
131+
function addRadio(options, modifier = '') {
132+
if (isEmpty(options)) {
127133
return
128134
}
129135

@@ -137,9 +143,13 @@ module.exports = function ({ addUtilities, addComponents, theme, postcss }) {
137143
},
138144
},
139145
}, options)
140-
}, icon || (iconColor => {
141-
return `<svg viewBox="0 0 16 16" fill="${iconColor}" xmlns="http://www.w3.org/2000/svg"><circle cx="8" cy="8" r="3"/></svg>`
142-
})))
146+
}, ({ icon = options.icon, iconColor = options.iconColor }) => {
147+
return {
148+
'&:checked': {
149+
backgroundImage: `url("${svgToDataUri(isFunction(icon) ? icon(iconColor) : icon)}")`
150+
}
151+
}
152+
}))
143153
}
144154

145155
function registerComponents() {

0 commit comments

Comments
 (0)