Skip to content

Commit ccdddc9

Browse files
committed
fixed colour picker finally
1 parent c2114e3 commit ccdddc9

5 files changed

Lines changed: 85 additions & 54 deletions

File tree

app/components/palettes/index.js

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React, { Component, useRef, useEffect, useState } from 'react';
22
import { environment } from '#store/environment';
33
import { observer } from 'mobx-react';
44
import {
@@ -14,43 +14,69 @@ const Handle = SortableHandle(({ lineIndex }) => (
1414
<div className="index">{lineIndex}</div>
1515
));
1616

17-
// anim, colour, placement
17+
const Color = ({ color, onChange, rect }) => {
18+
const [points, setPoints] = useState(['br', 'tr']);
19+
const [offset, setOffset] = useState([2, 2])
20+
const node = useRef();
21+
useEffect(() => {
22+
if (node.current) {
23+
const { x, y, height } = node.current.getBoundingClientRect();
24+
const overflowX = x + 250 > window.innerWidth;
25+
const overflowY = (y + height) + 250 > window.innerHeight;
26+
const xType = 'lr'[+overflowX];
27+
setPoints(['tb'[+overflowY]+xType, 'bt'[+overflowY]+xType])
28+
setOffset([overflowX ? -2 : 2, overflowY ? -3 : 3])
29+
}
30+
}, [node.current, rect]);
31+
return (
32+
<Trigger
33+
action={['click']}
34+
prefixCls="color-picker"
35+
popup={(
36+
<Picker
37+
color={color}
38+
onChange={onChange}
39+
/>
40+
)}
41+
popupAlign={{
42+
points,
43+
offset,
44+
targetOffset: [0, 0],
45+
// overflow: { adjustX: true, adjustY: true },
46+
}}
47+
destroyPopupOnHide
48+
>
49+
<div
50+
ref={node}
51+
className="color"
52+
style={{
53+
backgroundColor: color,
54+
textAlign: 'center',
55+
}}
56+
/>
57+
</Trigger>
58+
);
59+
};
60+
61+
// placement
1862

1963
const SortableItem = SortableElement(
20-
observer(({ line, lineIndex }) => {
64+
observer(({ line, lineIndex, rect }) => {
2165
return (
2266
<div className="line">
2367
<Handle lineIndex={lineIndex} />
2468
{line.map((color, colorIndex) => {
2569
return (
26-
<Trigger
70+
<Color
2771
key={colorIndex}
28-
action={['click']}
29-
prefixCls="color-picker"
30-
popup={(
31-
<Picker
32-
color={color}
33-
onChange={({ hex }) => {
34-
environment.palettes[lineIndex][
35-
colorIndex
36-
] = hexToMDHex(hex);
37-
}}
38-
/>
39-
)}
40-
popupAlign={{
41-
points: ['tl', 'bl'],
42-
offset: [0, 3]
72+
color={color}
73+
rect={rect}
74+
onChange={({ hex }) => {
75+
environment.palettes[lineIndex][
76+
colorIndex
77+
] = hexToMDHex(hex);
4378
}}
44-
destroyPopupOnHide
45-
>
46-
<div
47-
className="color"
48-
style={{
49-
backgroundColor: color,
50-
textAlign: 'center',
51-
}}
52-
/>
53-
</Trigger>
79+
/>
5480
);
5581
})}
5682
</div>
@@ -62,15 +88,16 @@ const SortableList = SortableContainer(
6288
observer(
6389
class extends Component {
6490
render() {
65-
const { items, vert } = this.props;
91+
const { items, rect } = this.props;
6692
return (
67-
<div className={`palettes ${vert ? 'vert' : ''}`}>
93+
<div className={`palettes ${rect.vert ? 'vert' : ''}`}>
6894
{items.map((line, index) => (
6995
<SortableItem
7096
key={`item-${index}`}
7197
index={index}
7298
line={line}
7399
lineIndex={index}
100+
rect={rect}
74101
/>
75102
))}
76103
</div>
@@ -83,25 +110,25 @@ const SortableList = SortableContainer(
83110

84111
@observer
85112
export class Palettes extends Component {
86-
state = { vert: false };
113+
state = { width: 0, height: 0, x: 0, y: 0, };
87114
mounted = false;
88115

89116
onRef = (node) => {
90117
if (node) {
91118
requestAnimationFrame(() => {
92-
const { width, height } = node.getBoundingClientRect();
93-
this.mounted && this.setState({ vert: width < height });
119+
const { width, height, x, y } = node.getBoundingClientRect();
120+
const vert = width < height;
121+
this.mounted && this.setState({ vert, width, height, x, y });
94122
});
95123
}
96124
};
97125

98126
componentDidMount() {
99127
this.mounted = true;
100128
this.props.node.setEventListener('resize', (e) => {
101-
const { width, height } = e.rect;
102-
129+
const vert = e.rect.width < e.rect.height;
103130
requestAnimationFrame(() => {
104-
this.mounted && this.setState({ vert: width < height });
131+
this.mounted && this.setState({ vert, ...e.rect });
105132
});
106133
});
107134
}
@@ -126,7 +153,7 @@ export class Palettes extends Component {
126153
items={palettes}
127154
lockToContainerEdges={true}
128155
useDragHandle={true}
129-
vert={vert}
156+
rect={this.state}
130157
onSortEnd={this.onSortEnd}
131158
/>
132159

app/components/palettes/picker.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -204,27 +204,14 @@ export const Sketch = ({
204204
},
205205
hue: {
206206
position: 'relative',
207-
height: '10px',
207+
height: '24px',
208208
overflow: 'hidden',
209209
},
210210
Hue: {
211211
radius: '2px',
212212
shadow:
213213
'inset 0 0 0 1px rgba(0,0,0,.15), inset 0 0 4px rgba(0,0,0,.25)',
214214
},
215-
216-
alpha: {
217-
position: 'relative',
218-
height: '10px',
219-
marginTop: '4px',
220-
overflow: 'hidden',
221-
display: 'none',
222-
},
223-
Alpha: {
224-
radius: '2px',
225-
shadow:
226-
'inset 0 0 0 1px rgba(0,0,0,.15), inset 0 0 4px rgba(0,0,0,.25)',
227-
},
228215
},
229216
});
230217

@@ -241,7 +228,22 @@ export const Sketch = ({
241228
<div style={styles.controls} className="flexbox-fix">
242229
<div style={styles.sliders}>
243230
<div style={styles.hue}>
244-
<Hue style={styles.Hue} hsl={hsl} onChange={onChange} />
231+
<Hue
232+
style={styles.Hue}
233+
hsl={hsl}
234+
onChange={onChange}
235+
height="24px"
236+
pointer={() => (
237+
<div style={{
238+
width: 4,
239+
height: 22,
240+
backgroundColor: white,
241+
borderRadius: '4px',
242+
pointerEvents: 'none',
243+
transform: 'translate(-2px, 1px)',
244+
}}/>
245+
)}
246+
/>
245247
</div>
246248
</div>
247249
<div style={styles.color}>

app/formats/scripts/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
// ability to specify offset & etc
3434
// allow editing filename
3535
// format:
36+
// add to project
3637
// remove acdeditor
3738
// purple warning: arbitrary code - warning before yes
3839
// derive UI from script - make dplcs still optional

development/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = (env = {}, args = {}) => {
1515
rules: [
1616
{
1717
test: /\.m?jsx?$/,
18-
// exclude: env.dev ? /node_modules/ : void 0,
18+
exclude: env.dev ? /node_modules/ : void 0,
1919
use: [
2020
{
2121
loader: 'babel-loader',

styles/components/palettes.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,5 @@
8282

8383
.color-picker {
8484
display: inline-block;
85+
z-index: $z-float;
8586
}

0 commit comments

Comments
 (0)