Skip to content

Commit b146e57

Browse files
committed
fix positioning issues, add css watcher
1 parent e2efc78 commit b146e57

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

img/compare.svg

Lines changed: 1 addition & 1 deletion
Loading

src/generator.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ export default function generateSvgSquircle(height, width, radius, clip) {
2929
mapRange(radius, (x / 2) * .90, x / 2, 0, 1),
3030
1
3131
)) * 1.7,
32-
hyOffset = yOffsetF(height),
33-
wyOffset = yOffsetF(width)
32+
hyOffset = clip !== false ? yOffsetF(height) : 0,
33+
wyOffset = clip !== false ? yOffsetF(width) : 0
3434

3535
const startPoint = `${a0xw},${wyOffset}`
3636

3737
return `M${startPoint}
38-
${width / 2 < a0x
38+
${width / 2 < a0x && clip !== false
3939
? ''
4040
: `L${width - a0xw},0`
4141
}
@@ -45,7 +45,7 @@ export default function generateSvgSquircle(height, width, radius, clip) {
4545
C${width},${a2x},${width},${a1x},
4646
4747
${width - hyOffset},${a0xh}
48-
${height / 2 < a0x
48+
${height / 2 < a0x && clip !== false
4949
? ''
5050
: `L${width},${height - a0xh}`
5151
}
@@ -55,7 +55,7 @@ export default function generateSvgSquircle(height, width, radius, clip) {
5555
C${width - a2x},${height},${width - a1x},${height},
5656
5757
${width - a0xw},${height - wyOffset}
58-
${width / 2 < a0x
58+
${width / 2 < a0x && clip !== false
5959
? ''
6060
: `L${a0xw},${height}`
6161
}
@@ -65,7 +65,7 @@ export default function generateSvgSquircle(height, width, radius, clip) {
6565
C0,${height - a2x},0,${height - a1x},
6666
6767
${hyOffset},${height - a0xh}
68-
${height / 2 < a0x
68+
${height / 2 < a0x && clip !== false
6969
? ''
7070
: `L0,${a0xh}`
7171
}

src/main.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import React, {useRef, useEffect, useState} from 'react'
1+
import React, {useRef, useEffect, useState, useCallback} from 'react'
22
import generateSvgSquircle from './generator'
33
import './getMatchedCSSRules-polyfill'
44
import updateStates from "./updateStates"
55
import ShadowRoot from "./react-shadow-dom"
6+
import attachCSSWatcher from './style-sheet-watcher'
67

78
export default function RoundDiv({clip, style, children, ...props}) {
89
const [height, setHeight] = useState(0)
@@ -22,7 +23,7 @@ export default function RoundDiv({clip, style, children, ...props}) {
2223
div.current?.attachShadow({mode: 'open'})
2324
}, [])
2425

25-
useEffect(() => updateStates({
26+
const updateStatesWithArgs = useCallback(() => updateStates({
2627
div,
2728
style,
2829
setHeight,
@@ -33,7 +34,13 @@ export default function RoundDiv({clip, style, children, ...props}) {
3334
setBorderColor,
3435
setBorderWidth,
3536
setBorderOpacity
36-
}), [div, clip, style])
37+
}), [style])
38+
39+
useEffect(updateStatesWithArgs, [div, clip, style, updateStatesWithArgs])
40+
41+
useEffect(() => {
42+
attachCSSWatcher(() => updateStatesWithArgs())
43+
}, [updateStatesWithArgs])
3744

3845
const divStyle = {
3946
...style
@@ -45,13 +52,14 @@ export default function RoundDiv({clip, style, children, ...props}) {
4552

4653
return <div {...props} style={divStyle} ref={div}>
4754
<ShadowRoot>
55+
<style>{':host{position:relative}'}</style>
4856
<svg viewBox={`0 0 ${width} ${height}`} style={{
49-
position: 'fixed',
57+
position: 'absolute',
5058
height,
51-
width,
59+
width: 1,
5260
overflow: 'visible',
5361
zIndex: -1
54-
}} xmlnsXlink="http://www.w3.org/1999/xlink">
62+
}} xmlnsXlink="http://www.w3.org/1999/xlink" preserveAspectRatio={'xMidYMid slice'}>
5563
<defs>
5664
<path d={
5765
generateSvgSquircle(height + borderWidth * 2, width + borderWidth * 2, radius, clip)

src/style-sheet-watcher.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
let watchers = 0
2+
const CSSChangeEvent = new CustomEvent('css-change');
3+
4+
export default function attachCSSWatcher(callback) {
5+
watchers++
6+
console.log(watchers)
7+
CSSWatcher.addEventListener('css-change', () => callback())
8+
}
9+
10+
const CSSWatcher = new EventTarget()
11+
12+
;(function watchCSS() {
13+
let CSS = getCSSText()
14+
setInterval(() => {
15+
const newCSS = getCSSText()
16+
if (CSS === newCSS) return
17+
CSS = newCSS
18+
CSSWatcher.dispatchEvent(CSSChangeEvent)
19+
}, 1000)
20+
})()
21+
22+
function getCSSText() {
23+
let CSS = ''
24+
for (let i = 0; i < document.styleSheets.length; i++) {
25+
const sheet = document.styleSheets[i]
26+
for (let j = 0; j < sheet.rules.length; j++) {
27+
CSS += sheet.rules[j].cssText
28+
}
29+
}
30+
return CSS
31+
}

0 commit comments

Comments
 (0)