Skip to content

Commit 46ce13b

Browse files
committed
bugfixes and improvements
add overflow support, prevent flash of sharp corners, better support for transitions in transform and height / width
1 parent 7811799 commit 46ce13b

File tree

5 files changed

+53
-31
lines changed

5 files changed

+53
-31
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ npm i react-round-div
2828

2929
| | Gzipped | Minified + Gzipped |
3030
|----------------------------------|---------:|-------------------:|
31-
| `react-round-div` | 7.71 KB | 5.18 KB |
32-
| `react-round-div` + dependencies | 14.88 KB | **9.07 KB** |
31+
| `react-round-div` | 7.81 KB | 5.27 KB |
32+
| `react-round-div` + dependencies | 21.01 KB | **9.45 KB** |
3333

3434
## Usage
3535

@@ -50,8 +50,6 @@ const App = () => {
5050
export default App;
5151
```
5252

53-
Also, [turn off or polyfill `url`, `path`, and `fs`](#webpack)
54-
5553
### Options
5654

5755
#### `dontConvertShadow`
@@ -76,6 +74,7 @@ There are a couple of css properties, that you can't reliably set _inline_ with
7674

7775
- the `background` property and all the properties it is a shorthand for
7876
- the `border`, `border-image`, and all the properties they are a shorthand for
77+
- `overflow`
7978
- `box-shadow`
8079
- `filter`, if you haven't set [`dontConvertShadow`](#dontconvertshadow)
8180

@@ -85,4 +84,5 @@ set [`dontConvertShadow`](#dontconvertshadow). This may also change in future ve
8584

8685
### Intended differences to the html `<div>`
8786

88-
Unlike the html `<div>`, `RoundDiv` still rounds the corners of borders of there is a border image set. This looks great with gradients, but might clash with the use of actual images as borders.
87+
Unlike the html `<div>`, `RoundDiv` still rounds the corners of borders if you have set a border image. This looks great with gradients, but might clash with the use of actual images as borders.
88+
You may notice a flash of white in the border before the gradient is shown. To combat this, define a `border-color` for the element in addition to the `border-image` gradient. This gives the browser a fallback to show before the `RoundDiv` script generates the smooth corners.

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-round-div",
3-
"version": "1.3.0",
3+
"version": "1.3.2",
44
"description": "Make your rounded corners look phenomenal with g2 continuity.",
55
"main": "dist/main.js",
66
"scripts": {

src/main.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default function RoundDiv({style, children, dontConvertShadow, ...props})
2020
const [width, setWidth] = useState(0)
2121
const [radius, setRadius] = useState(array(4, 0))
2222
const [boxSizing, setBoxSizing] = useState('content-box')
23+
const [overflow, setOverflow] = useState('visible')
2324

2425
const [transition, setTransition] = useState(array(4, 0))
2526

@@ -38,6 +39,7 @@ export default function RoundDiv({style, children, dontConvertShadow, ...props})
3839
div,
3940
style,
4041
setBoxSizing,
42+
setOverflow,
4143
setPadding,
4244
setHeight,
4345
setWidth,
@@ -52,7 +54,11 @@ export default function RoundDiv({style, children, dontConvertShadow, ...props})
5254

5355
useEffect(() => {
5456
const watcherId = attachCSSWatcher(updateStatesWithArgs, div.current) // todo: make this a react hook
55-
updateStatesWithArgs()
57+
const waitForDocumentComplete = setInterval(() => {
58+
if (document.readyState !== 'complete') return
59+
clearInterval(waitForDocumentComplete)
60+
updateStatesWithArgs()
61+
}, 1)
5662
return () => {
5763
detachCSSWatcher(watcherId)
5864
}
@@ -102,7 +108,9 @@ export default function RoundDiv({style, children, dontConvertShadow, ...props})
102108
const pathIsEmpty = path.startsWith('Z') || path === ''
103109
const divStyle = {
104110
...style,
105-
...(pathIsEmpty ? {} : {
111+
...(pathIsEmpty ? {
112+
borderImage: 'none',
113+
} : {
106114
...invisibleBorderStyles,
107115
borderImage: 'none',
108116
background: 'transparent',
@@ -112,6 +120,7 @@ export default function RoundDiv({style, children, dontConvertShadow, ...props})
112120
: 'none',
113121
// drop shadow only
114122
filter: dontConvertShadow ? '' : shadows[0].map(shadowData => `drop-shadow(${shadowData})`).join(' '),
123+
overflow: 'visible',
115124
})
116125
}
117126

@@ -128,7 +137,10 @@ export default function RoundDiv({style, children, dontConvertShadow, ...props})
128137

129138
const widenedBorderWidth = border.width.map(v => v + Math.max(.5, v * .1))
130139

131-
return <div {...props} style={divStyle} ref={div} data-rrd-overwritten={pathIsEmpty ? 'false' : 'true'}>
140+
if (div.current)
141+
div.current.rrdOverwritten = !pathIsEmpty
142+
143+
return <div {...props} style={divStyle} ref={div}>
132144
{pathIsEmpty ? null : <ShadowRoot>
133145
<div style={{
134146
transform: 'scale(1)'
@@ -168,7 +180,15 @@ export default function RoundDiv({style, children, dontConvertShadow, ...props})
168180
transition,
169181
}}/>
170182
</div>
171-
<slot style={{overflow: 'visible'}}/>
183+
<div style={{
184+
height: height - (border.width[0] + border.width[2]) * 2,
185+
width: width - (border.width[1] + border.width[3]) * 2,
186+
...invisibleBorderStyles,
187+
transform: `translate(-${padding[3] + border.width[3]}px, -${padding[0] + border.width[0]}px)`,
188+
clipPath: overflow === 'hidden' ? `path("${innerPath}")` : null,
189+
}}>
190+
<slot style={{overflow: 'visible'}}/>
191+
</div>
172192
</div>
173193
</ShadowRoot>}
174194
{children}

src/updateStates.js

+21-19
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@ import ReactDOM from 'react-dom'
88
import {lazySetArrayState, lazySetObjectsState} from './utils/react-utils'
99

1010
export default function updateStates(args) {
11-
const {div, setHeight, setWidth} = args
12-
const boundingClientRect = div.current?.getBoundingClientRect()
13-
let height, width;
14-
if (boundingClientRect) {
15-
height = boundingClientRect.height
16-
width = boundingClientRect.width
17-
setHeight(height)
18-
setWidth(width)
19-
}
11+
const {div} = args
12+
if (!div.current) return
2013

2114
const getNthStyle = (key, n, shorthand) => {
2215
const styles = getAllCssPropertyDeclarationsForElement(key, div.current, shorthand)
@@ -33,7 +26,7 @@ export default function updateStates(args) {
3326
lazySetBorderImage = newState => lazySetObjectsState(states.setBorderImage, newState),
3427
lazySetShadows = newState => lazySetArrayState(states.setShadows, newState)
3528

36-
const oneIfStylesAreOverridden = args.div?.current?.dataset.rrdOverwritten === 'true' ? 1 : 0
29+
const oneIfStylesAreOverridden = args.div?.current?.rrdOverwritten ? 1 : 0
3730

3831
ReactDOM.unstable_batchedUpdates(() => {
3932
states.setPadding(
@@ -42,15 +35,6 @@ export default function updateStates(args) {
4235
.map(n => toNumber(n, div.current) || 0)
4336
)
4437

45-
lazySetRadius(
46-
getBorderRadii(0)
47-
.map(s => Math.min(
48-
toNumber(s, div.current),
49-
height / 2,
50-
width / 2
51-
))
52-
)
53-
5438
states.setTransition(
5539
getNthStyle('transition', 0)
5640
)
@@ -59,6 +43,10 @@ export default function updateStates(args) {
5943
getNthStyle('box-sizing', 0) || 'content-box'
6044
)
6145

46+
states.setOverflow(
47+
getNthStyle('overflow', oneIfStylesAreOverridden) || 'visible'
48+
)
49+
6250
lazySetBackground(
6351
Object.fromEntries([
6452
'attachment',
@@ -94,6 +82,20 @@ export default function updateStates(args) {
9482
border.width = border.width ?? Array(4).fill(0)
9583
lazySetBorder(border)
9684

85+
const height = div.current?.clientHeight + border.width[0] + border.width[2],
86+
width = div.current?.clientWidth + border.width[1] + border.width[3]
87+
states.setHeight(height)
88+
states.setWidth(width)
89+
90+
lazySetRadius(
91+
getBorderRadii(0)
92+
.map(s => Math.min(
93+
toNumber(s, div.current),
94+
height / 2,
95+
width / 2
96+
))
97+
)
98+
9799
lazySetBorderImage(
98100
Object.fromEntries([
99101
'outset',

0 commit comments

Comments
 (0)