Skip to content

Commit f4e32a1

Browse files
authored
Transparent Swatches (#354)
* Add Transparent Swatch Option * Add Transparent to the Docs * Update Snapshot Tests
1 parent 2d41941 commit f4e32a1

File tree

19 files changed

+476
-128
lines changed

19 files changed

+476
-128
lines changed

docs/build/bundle.js

Lines changed: 87 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26262,7 +26262,7 @@
2626226262

2626326263
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = Alpha.__proto__ || Object.getPrototypeOf(Alpha)).call.apply(_ref2, [this].concat(args))), _this), _this.handleChange = function (e, skip) {
2626426264
var change = alpha.calculateChange(e, skip, _this.props, _this.refs.container);
26265-
change && _this.props.onChange(change, e);
26265+
change && _this.props.onChange && _this.props.onChange(change, e);
2626626266
}, _this.handleMouseDown = function (e) {
2626726267
_this.handleChange(e, true);
2626826268
window.addEventListener('mousemove', _this.handleChange);
@@ -26459,11 +26459,15 @@
2645926459
var white = _ref.white,
2646026460
grey = _ref.grey,
2646126461
size = _ref.size,
26462-
renderers = _ref.renderers;
26462+
renderers = _ref.renderers,
26463+
borderRadius = _ref.borderRadius,
26464+
boxShadow = _ref.boxShadow;
2646326465

2646426466
var styles = (0, _reactcss2.default)({
2646526467
'default': {
2646626468
grid: {
26469+
borderRadius: borderRadius,
26470+
boxShadow: boxShadow,
2646726471
absolute: '0px 0px 0px 0px',
2646826472
background: 'url(' + checkboard.get(white, grey, size, renderers.canvas) + ') center left'
2646926473
}
@@ -26569,9 +26573,9 @@
2656926573

2657026574
_this.handleChange = function (e) {
2657126575
if (!!_this.props.label) {
26572-
_this.props.onChange(_defineProperty({}, _this.props.label, e.target.value), e);
26576+
_this.props.onChange && _this.props.onChange(_defineProperty({}, _this.props.label, e.target.value), e);
2657326577
} else {
26574-
_this.props.onChange(e.target.value, e);
26578+
_this.props.onChange && _this.props.onChange(e.target.value, e);
2657526579
}
2657626580

2657726581
_this.setState({ value: e.target.value });
@@ -26585,9 +26589,9 @@
2658526589
// Up
2658626590
if (e.keyCode === 38) {
2658726591
if (_this.props.label !== null) {
26588-
_this.props.onChange(_defineProperty({}, _this.props.label, number + amount), e);
26592+
_this.props.onChange && _this.props.onChange(_defineProperty({}, _this.props.label, number + amount), e);
2658926593
} else {
26590-
_this.props.onChange(number + amount, e);
26594+
_this.props.onChange && _this.props.onChange(number + amount, e);
2659126595
}
2659226596

2659326597
_this.setState({ value: number + amount });
@@ -26596,9 +26600,9 @@
2659626600
// Down
2659726601
if (e.keyCode === 40) {
2659826602
if (_this.props.label !== null) {
26599-
_this.props.onChange(_defineProperty({}, _this.props.label, number - amount), e);
26603+
_this.props.onChange && _this.props.onChange(_defineProperty({}, _this.props.label, number - amount), e);
2660026604
} else {
26601-
_this.props.onChange(number - amount, e);
26605+
_this.props.onChange && _this.props.onChange(number - amount, e);
2660226606
}
2660326607

2660426608
_this.setState({ value: number - amount });
@@ -26610,7 +26614,7 @@
2661026614
if (_this.props.dragLabel) {
2661126615
var newValue = Math.round(_this.props.value + e.movementX);
2661226616
if (newValue >= 0 && newValue <= _this.props.dragMax) {
26613-
_this.props.onChange(_defineProperty({}, _this.props.label, newValue), e);
26617+
_this.props.onChange && _this.props.onChange(_defineProperty({}, _this.props.label, newValue), e);
2661426618
}
2661526619
}
2661626620
};
@@ -26757,7 +26761,7 @@
2675726761

2675826762
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = Hue.__proto__ || Object.getPrototypeOf(Hue)).call.apply(_ref2, [this].concat(args))), _this), _this.handleChange = function (e, skip) {
2675926763
var change = hue.calculateChange(e, skip, _this.props, _this.refs.container);
26760-
change && _this.props.onChange(change, e);
26764+
change && _this.props.onChange && _this.props.onChange(change, e);
2676126765
}, _this.handleMouseDown = function (e) {
2676226766
_this.handleChange(e, true);
2676326767
window.addEventListener('mousemove', _this.handleChange);
@@ -26957,7 +26961,7 @@
2695726961
var _this = _possibleConstructorReturn(this, (Saturation.__proto__ || Object.getPrototypeOf(Saturation)).call(this, props));
2695826962

2695926963
_this.handleChange = function (e, skip) {
26960-
_this.throttle(_this.props.onChange, saturation.calculateChange(e, skip, _this.props, _this.refs.container), e);
26964+
_this.props.onChange && _this.throttle(_this.props.onChange, saturation.calculateChange(e, skip, _this.props, _this.refs.container), e);
2696126965
};
2696226966

2696326967
_this.handleMouseDown = function (e) {
@@ -27530,9 +27534,7 @@
2753027534
}
2753127535
};
2753227536

27533-
_this.state = _extends({}, _color2.default.toState(props.color, 0), {
27534-
visible: props.display
27535-
});
27537+
_this.state = _extends({}, _color2.default.toState(props.color, 0));
2753627538

2753727539
_this.debounce = (0, _debounce2.default)(function (fn, data, event) {
2753827540
fn(data, event);
@@ -27543,9 +27545,7 @@
2754327545
_createClass(ColorPicker, [{
2754427546
key: 'componentWillReceiveProps',
2754527547
value: function componentWillReceiveProps(nextProps) {
27546-
this.setState(_extends({}, _color2.default.toState(nextProps.color, this.state.oldHue), {
27547-
visible: nextProps.display
27548-
}));
27548+
this.setState(_extends({}, _color2.default.toState(nextProps.color, this.state.oldHue)));
2754927549
}
2755027550
}, {
2755127551
key: 'render',
@@ -27611,15 +27611,18 @@
2761127611
var color = data.hex ? (0, _tinycolor2.default)(data.hex) : (0, _tinycolor2.default)(data);
2761227612
var hsl = color.toHsl();
2761327613
var hsv = color.toHsv();
27614+
var rgb = color.toRgb();
27615+
var hex = color.toHex();
2761427616
if (hsl.s === 0) {
2761527617
hsl.h = oldHue || 0;
2761627618
hsv.h = oldHue || 0;
2761727619
}
27620+
var transparent = hex === '000000' && rgb.a === 0;
2761827621

2761927622
return {
2762027623
hsl: hsl,
27621-
hex: '#' + color.toHex(),
27622-
rgb: color.toRgb(),
27624+
hex: transparent ? 'transparent' : '#' + hex,
27625+
rgb: rgb,
2762327626
hsv: hsv,
2762427627
oldHue: data.h || oldHue || hsl.h,
2762527628
source: data.source
@@ -28873,6 +28876,8 @@
2887328876
});
2887428877
exports.Swatch = undefined;
2887528878

28879+
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
28880+
2887628881
var _react = __webpack_require__(2);
2887728882

2887828883
var _react2 = _interopRequireDefault(_react);
@@ -28881,34 +28886,45 @@
2888128886

2888228887
var _reactcss2 = _interopRequireDefault(_reactcss);
2888328888

28889+
var _ = __webpack_require__(336);
28890+
2888428891
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2888528892

2888628893
var Swatch = exports.Swatch = function Swatch(_ref) {
2888728894
var color = _ref.color,
2888828895
style = _ref.style,
28889-
onClick = _ref.onClick,
28896+
_ref$onClick = _ref.onClick,
28897+
onClick = _ref$onClick === undefined ? function () {} : _ref$onClick,
2889028898
_ref$title = _ref.title,
28891-
title = _ref$title === undefined ? color : _ref$title;
28899+
title = _ref$title === undefined ? color : _ref$title,
28900+
children = _ref.children;
2889228901

28902+
var transparent = color === 'transparent';
2889328903
var styles = (0, _reactcss2.default)({
28894-
'default': {
28895-
swatch: {
28904+
default: {
28905+
swatch: _extends({
2889628906
background: color,
2889728907
height: '100%',
2889828908
width: '100%',
28899-
cursor: 'pointer'
28900-
}
28901-
},
28902-
'custom': {
28903-
swatch: style
28909+
cursor: 'pointer',
28910+
position: 'relative'
28911+
}, style)
2890428912
}
28905-
}, 'custom');
28913+
});
2890628914

2890728915
var handleClick = function handleClick(e) {
2890828916
return onClick(color, e);
2890928917
};
2891028918

28911-
return _react2.default.createElement('div', { style: styles.swatch, onClick: handleClick, title: title });
28919+
return _react2.default.createElement(
28920+
'div',
28921+
{ style: styles.swatch, onClick: handleClick, title: title },
28922+
children,
28923+
transparent && _react2.default.createElement(_.Checkboard, {
28924+
borderRadius: styles.swatch.borderRadius,
28925+
boxShadow: 'inset 0 0 0 1px rgba(0,0,0,0.1)'
28926+
})
28927+
);
2891228928
};
2891328929

2891428930
exports.default = Swatch;
@@ -28998,6 +29014,7 @@
2899829014
width = _ref.width,
2899929015
triangle = _ref.triangle;
2900029016

29017+
var transparent = hex === 'transparent';
2900129018
var handleChange = function handleChange(hexCode, e) {
2900229019
_color2.default.isValidHex(hexCode) && onChange({
2900329020
hex: hexCode,
@@ -29020,14 +29037,16 @@
2902029037
borderRadius: '6px 6px 0 0',
2902129038
display: 'flex',
2902229039
alignItems: 'center',
29023-
justifyContent: 'center'
29040+
justifyContent: 'center',
29041+
position: 'relative'
2902429042
},
2902529043
body: {
2902629044
padding: '10px'
2902729045
},
2902829046
label: {
2902929047
fontSize: '18px',
29030-
color: '#fff'
29048+
color: transparent ? 'rgba(0,0,0,0.4)' : '#fff',
29049+
position: 'relative'
2903129050
},
2903229051
triangle: {
2903329052
width: '0px',
@@ -29067,6 +29086,7 @@
2906729086
_react2.default.createElement(
2906829087
'div',
2906929088
{ style: styles.head },
29089+
transparent && _react2.default.createElement(_common.Checkboard, { borderRadius: '6px 6px 0 0' }),
2907029090
_react2.default.createElement(
2907129091
'div',
2907229092
{ style: styles.label },
@@ -30989,11 +31009,14 @@
3098931009

3099031010
var _reactcss2 = _interopRequireDefault(_reactcss);
3099131011

31012+
var _common = __webpack_require__(336);
31013+
3099231014
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
3099331015

3099431016
var CompactColor = exports.CompactColor = function CompactColor(_ref) {
3099531017
var color = _ref.color,
30996-
onClick = _ref.onClick,
31018+
_ref$onClick = _ref.onClick,
31019+
onClick = _ref$onClick === undefined ? function () {} : _ref$onClick,
3099731020
active = _ref.active;
3099831021

3099931022
var styles = (0, _reactcss2.default)({
@@ -31027,16 +31050,17 @@
3102731050
dot: {
3102831051
background: '#000'
3102931052
}
31053+
},
31054+
'transparent': {
31055+
dot: {
31056+
background: '#000'
31057+
}
3103031058
}
31031-
}, { active: active, 'color-#FFFFFF': color === '#FFFFFF' });
31032-
31033-
var handleClick = function handleClick(e) {
31034-
return onClick({ hex: color }, e);
31035-
};
31059+
}, { active: active, 'color-#FFFFFF': color === '#FFFFFF', 'transparent': color === 'transparent' });
3103631060

3103731061
return _react2.default.createElement(
31038-
'div',
31039-
{ style: styles.color, onClick: handleClick },
31062+
_common.Swatch,
31063+
{ style: styles.color, color: color, onClick: onClick },
3104031064
_react2.default.createElement('div', { style: styles.dot })
3104131065
);
3104231066
};
@@ -32701,7 +32725,8 @@
3270132725

3270232726
var SketchPresetColors = exports.SketchPresetColors = function SketchPresetColors(_ref) {
3270332727
var colors = _ref.colors,
32704-
onClick = _ref.onClick;
32728+
_ref$onClick = _ref.onClick,
32729+
onClick = _ref$onClick === undefined ? function () {} : _ref$onClick;
3270532730

3270632731
var styles = (0, _reactcss2.default)({
3270732732
'default': {
@@ -32970,7 +32995,8 @@
3297032995
var SliderSwatch = exports.SliderSwatch = function SliderSwatch(_ref) {
3297132996
var hsl = _ref.hsl,
3297232997
offset = _ref.offset,
32973-
onClick = _ref.onClick,
32998+
_ref$onClick = _ref.onClick,
32999+
onClick = _ref$onClick === undefined ? function () {} : _ref$onClick,
3297433000
active = _ref.active,
3297533001
first = _ref.first,
3297633002
last = _ref.last;
@@ -33249,11 +33275,14 @@
3324933275

3325033276
var _reactcss2 = _interopRequireDefault(_reactcss);
3325133277

33278+
var _common = __webpack_require__(336);
33279+
3325233280
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
3325333281

3325433282
var SwatchesColor = exports.SwatchesColor = function SwatchesColor(_ref) {
3325533283
var color = _ref.color,
33256-
onClick = _ref.onClick,
33284+
_ref$onClick = _ref.onClick,
33285+
onClick = _ref$onClick === undefined ? function () {} : _ref$onClick,
3325733286
first = _ref.first,
3325833287
last = _ref.last,
3325933288
active = _ref.active;
@@ -33297,16 +33326,23 @@
3329733326
check: {
3329833327
fill: '#333'
3329933328
}
33329+
},
33330+
'transparent': {
33331+
check: {
33332+
fill: '#333'
33333+
}
3330033334
}
33301-
}, { first: first, last: last, active: active, 'color-#FFFFFF': color === '#FFFFFF' });
33302-
33303-
var handleClick = function handleClick(e) {
33304-
return onClick(color, e);
33305-
};
33335+
}, {
33336+
first: first,
33337+
last: last,
33338+
active: active,
33339+
'color-#FFFFFF': color === '#FFFFFF',
33340+
'transparent': color === 'transparent'
33341+
});
3330633342

3330733343
return _react2.default.createElement(
33308-
'div',
33309-
{ style: styles.color, onClick: handleClick },
33344+
_common.Swatch,
33345+
{ color: color, style: styles.color, onClick: onClick },
3331033346
_react2.default.createElement(
3331133347
'div',
3331233348
{ style: styles.check },
@@ -40770,7 +40806,7 @@
4077040806
/* 426 */
4077140807
/***/ function(module, exports) {
4077240808

40773-
module.exports = "---\nid: api-color\ntitle: color\n---\nColor controls what color is active on the color picker. You can use this to initialize the color picker with a particular color, or to keep it in sync with the state of a parent component.\n\nColor accepts either a string of a hex color `'#333'` or a object of rgb or hsl values `{ r: 51, g: 51, b: 51 }` or `{ h: 0, s: 0, l: .10 }`. Both rgb and hsl will also take a `a: 1` value for alpha.\n\n```\nimport React from 'react';\nimport { SketchPicker } from 'react-color';\n\nclass Component extends React.Component {\n state = {\n background: '#fff',\n };\n\n handleChangeComplete = (color) => {\n this.setState({ background: color.hex });\n };\n\n render() {\n return (\n <SketchPicker\n color={ this.state.background }\n onChangeComplete={ this.handleChangeComplete }\n />\n );\n }\n}\n```\nIn this case, the color picker will initialize with the color `#fff`. When the color is changed, `handleChangeComplete` will fire and set the new color to state.\n";
40809+
module.exports = "---\nid: api-color\ntitle: color\n---\nColor controls what color is active on the color picker. You can use this to initialize the color picker with a particular color, or to keep it in sync with the state of a parent component.\n\nColor accepts either a string of a hex color `'#333'` or a object of rgb or hsl values `{ r: 51, g: 51, b: 51 }` or `{ h: 0, s: 0, l: .10 }`. Both rgb and hsl will also take a `a: 1` value for alpha. You can also use `transparent`.\n\n```\nimport React from 'react';\nimport { SketchPicker } from 'react-color';\n\nclass Component extends React.Component {\n state = {\n background: '#fff',\n };\n\n handleChangeComplete = (color) => {\n this.setState({ background: color.hex });\n };\n\n render() {\n return (\n <SketchPicker\n color={ this.state.background }\n onChangeComplete={ this.handleChangeComplete }\n />\n );\n }\n}\n```\nIn this case, the color picker will initialize with the color `#fff`. When the color is changed, `handleChangeComplete` will fire and set the new color to state.\n";
4077440810

4077540811
/***/ },
4077640812
/* 427 */

docs/documentation/03.01-color.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: color
44
---
55
Color controls what color is active on the color picker. You can use this to initialize the color picker with a particular color, or to keep it in sync with the state of a parent component.
66

7-
Color accepts either a string of a hex color `'#333'` or a object of rgb or hsl values `{ r: 51, g: 51, b: 51 }` or `{ h: 0, s: 0, l: .10 }`. Both rgb and hsl will also take a `a: 1` value for alpha.
7+
Color accepts either a string of a hex color `'#333'` or a object of rgb or hsl values `{ r: 51, g: 51, b: 51 }` or `{ h: 0, s: 0, l: .10 }`. Both rgb and hsl will also take a `a: 1` value for alpha. You can also use `transparent`.
88

99
```
1010
import React from 'react';

src/components/alpha/__snapshots__/spec.js.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,19 @@ exports[`test Alpha renders correctly 1`] = `
3737
<div
3838
style={
3939
Object {
40+
"MozBorderRadius": undefined,
41+
"MozBoxShadow": undefined,
42+
"OBorderRadius": undefined,
43+
"OBoxShadow": undefined,
44+
"WebkitBorderRadius": undefined,
45+
"WebkitBoxShadow": undefined,
4046
"background": "url(null) center left",
47+
"borderRadius": undefined,
4148
"bottom": "0px",
49+
"boxShadow": undefined,
4250
"left": "0px",
51+
"msBorderRadius": undefined,
52+
"msBoxShadow": undefined,
4353
"position": "absolute",
4454
"right": "0px",
4555
"top": "0px",
@@ -154,9 +164,19 @@ exports[`test Alpha renders vertically 1`] = `
154164
<div
155165
style={
156166
Object {
167+
"MozBorderRadius": undefined,
168+
"MozBoxShadow": undefined,
169+
"OBorderRadius": undefined,
170+
"OBoxShadow": undefined,
171+
"WebkitBorderRadius": undefined,
172+
"WebkitBoxShadow": undefined,
157173
"background": "url(null) center left",
174+
"borderRadius": undefined,
158175
"bottom": "0px",
176+
"boxShadow": undefined,
159177
"left": "0px",
178+
"msBorderRadius": undefined,
179+
"msBoxShadow": undefined,
160180
"position": "absolute",
161181
"right": "0px",
162182
"top": "0px",

0 commit comments

Comments
 (0)