Skip to content

Commit cd55db2

Browse files
authored
[Chore] Implement onColorStopSelect
2 parents dbd42a1 + a56194b commit cd55db2

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-linear-gradient-picker",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "React linear gradient picker",
55
"main": "dist/index.js",
66
"scripts": {

src/components/ColorStop/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ const ColorStop = ({ stop, limits, onPosChange, onDeleteColor, onDragStart = noo
1616
colorStopRef
1717
});
1818

19-
const { offset, color, isActive } = stop;
19+
const { offset, color, isActive, opacity } = stop;
2020

2121
return (
2222
<div className={isActive ? 'cs active' : 'cs'}
2323
ref={colorStopRef}
2424
style={{ left: offset }}
2525
onMouseDown={drag}
2626
onTouchStart={drag}>
27-
<div style={{ backgroundColor: color }}/>
27+
<div style={{ backgroundColor: color, opacity }}/>
2828
</div>
2929
);
3030
};

src/components/GradientPicker/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React, { useState, useMemo } from 'react';
1+
import React, { useState, useMemo, useEffect } from 'react';
22
import ColorStopsHolder from '../ColorStopsHolder/index';
33
import Palette from '../Palette/index';
44
import ColorPicker from '../ColorPicker/index';
55
import { GRADIENT_PICKER_PROP_TYPES } from '../propTypes/index';
6-
import { sortPalette } from '../../lib/index';
6+
import { sortPalette, noop } from '../../lib/index';
77
import {
88
HALF_STOP_WIDTH,
99
DEFAULT_HEIGHT,
@@ -29,7 +29,8 @@ const mapPaletteToStops = ({ palette, activeId, width }) => palette.map((color)
2929
}));
3030

3131
const getPaletteColor = (palette, id) => {
32-
const color = palette.find(color => color.id === id);
32+
const color = palette.find(color => color.id === id) || palette[0];
33+
3334
return { ...color, offset: Number(color.offset) };
3435
};
3536

@@ -42,7 +43,8 @@ const GradientPicker = ({
4243
maxStops = DEFAULT_MAX_STOPS,
4344
children,
4445
flatStyle = false,
45-
onPaletteChange
46+
onPaletteChange,
47+
onColorStopSelect = noop
4648
}) => {
4749
palette = mapIdToPalette(palette);
4850

@@ -79,7 +81,12 @@ const GradientPicker = ({
7981
};
8082

8183
const onStopDragStart = (id) => {
82-
setActiveColorId(id);
84+
if (id !== activeColorId) {
85+
setActiveColorId(id);
86+
87+
const color = palette.find((color) => color.id === id);
88+
onColorStopSelect(color);
89+
}
8390
};
8491

8592
const handleColorSelect = (color, opacity = 1) => {

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
path: path.join(__dirname, 'dist'),
1515
filename: 'index.js',
1616
library: 'linearGradientPicker',
17-
libraryTarget: 'umd'
17+
libraryTarget: 'commonjs2'
1818
},
1919
module: {
2020
rules: [

0 commit comments

Comments
 (0)