Skip to content

Commit eb204bb

Browse files
committed
improve rendering correct dimensions
1 parent 46ce13b commit eb204bb

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

src/main.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ export default function RoundDiv({style, children, dontConvertShadow, ...props})
3333
const [innerPath, setInnerPath] = useState('Z')
3434

3535
const div = useRef()
36+
const slotWrapper = useRef()
3637

3738
const updateStatesWithArgs = () => {
3839
updateStates({
3940
div,
41+
slotWrapper,
4042
style,
4143
setBoxSizing,
4244
setOverflow,
@@ -108,11 +110,10 @@ export default function RoundDiv({style, children, dontConvertShadow, ...props})
108110
const pathIsEmpty = path.startsWith('Z') || path === ''
109111
const divStyle = {
110112
...style,
111-
...(pathIsEmpty ? {
112-
borderImage: 'none',
113-
} : {
113+
borderImage: 'none',
114+
...(pathIsEmpty ? {} : {
114115
...invisibleBorderStyles,
115-
borderImage: 'none',
116+
padding: '0',
116117
background: 'transparent',
117118
boxShadow: dontConvertShadow
118119
// "box-shadow" must be overridden for the style extraction to work (with nth: 1, and not nth: 0)
@@ -125,13 +126,12 @@ export default function RoundDiv({style, children, dontConvertShadow, ...props})
125126
}
126127

127128
const shapeComponentStyles = {
128-
height: boxSizing === 'border-box' ? height : height - (border.width[0] + padding[0] + border.width[2] + padding[2]),
129-
width: boxSizing === 'border-box' ? width : width - (border.width[1] + padding[1] + border.width[3] + padding[3]),
130-
padding: padding.map(n => n + 'px').join(' '),
129+
height: boxSizing === 'border-box' ? height : height - (border.width[0] + border.width[2]),
130+
width: boxSizing === 'border-box' ? width : width - (border.width[1] + border.width[3]),
131131
position: 'fixed',
132132
left: 0,
133133
top: 0,
134-
transform: `translate(-${padding[3] + border.width[3]}px, -${padding[0] + border.width[0]}px)`,
134+
transform: `translate(-${border.width[3]}px, -${border.width[0]}px)`,
135135
zIndex: -1,
136136
}
137137

@@ -142,9 +142,7 @@ export default function RoundDiv({style, children, dontConvertShadow, ...props})
142142

143143
return <div {...props} style={divStyle} ref={div}>
144144
{pathIsEmpty ? null : <ShadowRoot>
145-
<div style={{
146-
transform: 'scale(1)'
147-
}}>
145+
<div style={{transform: 'scale(1)'}}>
148146
<div style={{
149147
...shapeComponentStyles,
150148
...invisibleBorderStyles,
@@ -165,7 +163,7 @@ export default function RoundDiv({style, children, dontConvertShadow, ...props})
165163
<div style={{
166164
height: height - (widenedBorderWidth[0] + widenedBorderWidth[2]),
167165
width: width - (widenedBorderWidth[1] + widenedBorderWidth[3]),
168-
transform: `translate(-${padding[3] + border.width[3]}px, -${padding[0] + border.width[0]}px)`,
166+
transform: `translate(-${border.width[3]}px, -${border.width[0]}px)`,
169167
clipPath: `path("M0,0V${height}H${width}V0Z${innerPath}")`,
170168
borderRadius: radius.map(n => (n * .95) + 'px').join(' '),
171169
borderColor: border.color,
@@ -181,13 +179,14 @@ export default function RoundDiv({style, children, dontConvertShadow, ...props})
181179
}}/>
182180
</div>
183181
<div style={{
184-
height: height - (border.width[0] + border.width[2]) * 2,
185-
width: width - (border.width[1] + border.width[3]) * 2,
182+
display: 'inline-flex',
186183
...invisibleBorderStyles,
187-
transform: `translate(-${padding[3] + border.width[3]}px, -${padding[0] + border.width[0]}px)`,
184+
padding: padding.map(n => n + 'px').join(' '),
188185
clipPath: overflow === 'hidden' ? `path("${innerPath}")` : null,
189-
}}>
190-
<slot style={{overflow: 'visible'}}/>
186+
transform: `translate(-${border.width[3]}px, -${border.width[0]}px)`,
187+
boxSizing: 'border-box',
188+
}} ref={slotWrapper}>
189+
<slot/>
191190
</div>
192191
</div>
193192
</ShadowRoot>}

src/updateStates.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import ReactDOM from 'react-dom'
88
import {lazySetArrayState, lazySetObjectsState} from './utils/react-utils'
99

1010
export default function updateStates(args) {
11-
const {div} = args
11+
const {div, slotWrapper} = args
1212
if (!div.current) return
1313

1414
const getNthStyle = (key, n, shorthand) => {
@@ -31,7 +31,7 @@ export default function updateStates(args) {
3131
ReactDOM.unstable_batchedUpdates(() => {
3232
states.setPadding(
3333
['top', 'right', 'bottom', 'left']
34-
.map(s => getNthStyle('padding-' + s, 0, 'padding'))
34+
.map(s => getNthStyle('padding-' + s, oneIfStylesAreOverridden, 'padding'))
3535
.map(n => toNumber(n, div.current) || 0)
3636
)
3737

@@ -82,8 +82,8 @@ export default function updateStates(args) {
8282
border.width = border.width ?? Array(4).fill(0)
8383
lazySetBorder(border)
8484

85-
const height = div.current?.clientHeight + border.width[0] + border.width[2],
86-
width = div.current?.clientWidth + border.width[1] + border.width[3]
85+
const height = ((slotWrapper.current ?? div.current).clientHeight || 0) + border.width[0] + border.width[2],
86+
width = ((slotWrapper.current ?? div.current).clientWidth || 0) + border.width[1] + border.width[3]
8787
states.setHeight(height)
8888
states.setWidth(width)
8989

@@ -104,7 +104,7 @@ export default function updateStates(args) {
104104
'source',
105105
'width',
106106
].map(key =>
107-
[key, getNthStyle('border-image-' + key, oneIfStylesAreOverridden, 'border-image')]
107+
[key, getNthStyle('border-image-' + key, 1, 'border-image')]
108108
))
109109
)
110110

0 commit comments

Comments
 (0)