Skip to content

Commit 670b029

Browse files
Shenatomarlonicus
authored andcommitted
Added support for foreignObject in our custom renderer and a test alongside it (#134)
1 parent e22975d commit 670b029

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

packages/@react-facet/dom-fiber/src/setupHostConfig.spec.tsx

+69
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ describe('mount', () => {
108108
})
109109

110110
describe('for svg', () => {
111+
it('renders foreignObject and sets dimensions', () => {
112+
render(
113+
<svg>
114+
<foreignObject x="50" y="100" width="150" height="200" />
115+
</svg>,
116+
)
117+
expect(root?.innerHTML ?? '').toBe(
118+
'<svg><foreignObject height="200" x="50" width="150" y="100"></foreignObject></svg>',
119+
)
120+
})
121+
111122
it('sets the d', () => {
112123
render(<fast-path d="M0,0 L0,10 Z" />)
113124
expect(root?.innerHTML ?? '').toBe('<path d="M0,0 L0,10 Z"></path>')
@@ -497,6 +508,22 @@ describe('mount', () => {
497508
})
498509

499510
describe('for svg', () => {
511+
it('renders foreignObject and sets dimensions', () => {
512+
const xFacet = createFacet({ initialValue: '50' })
513+
const yFacet = createFacet({ initialValue: '100' })
514+
const widthFacet = createFacet({ initialValue: '150' })
515+
const heightFacet = createFacet({ initialValue: '200' })
516+
517+
render(
518+
<svg>
519+
<fast-foreignObject x={xFacet} y={yFacet} width={widthFacet} height={heightFacet} />
520+
</svg>,
521+
)
522+
expect(root?.innerHTML ?? '').toBe(
523+
'<svg><foreignObject height="200" x="50" width="150" y="100"></foreignObject></svg>',
524+
)
525+
})
526+
500527
it('sets the d', () => {
501528
const dFacet = createFacet({ initialValue: 'M0,0 L0,10 Z' })
502529

@@ -1239,6 +1266,48 @@ describe('update', () => {
12391266
})
12401267

12411268
describe('for svg', () => {
1269+
it('updates foreignObject dimensions', () => {
1270+
const MockComponent = () => {
1271+
const [xFacet, setXFacet] = useState<string | undefined>('50')
1272+
const [yFacet, setYFacet] = useState<string | undefined>('100')
1273+
const [widthFacet, setWidthFacet] = useState<string | undefined>('150')
1274+
const [heightFacet, setHeightFacet] = useState<string | undefined>('200')
1275+
1276+
useEffect(() => {
1277+
setTimeout(() => setXFacet('350'), 1)
1278+
setTimeout(() => setYFacet('400'), 2)
1279+
setTimeout(() => setWidthFacet('450'), 3)
1280+
setTimeout(() => setHeightFacet('500'), 4)
1281+
}, [])
1282+
return (
1283+
<svg>
1284+
<fast-foreignObject x={xFacet} y={yFacet} width={widthFacet} height={heightFacet} />
1285+
</svg>
1286+
)
1287+
}
1288+
1289+
render(<MockComponent />)
1290+
expect(root?.innerHTML ?? '').toBe(
1291+
'<svg><foreignObject height="200" x="50" width="150" y="100"></foreignObject></svg>',
1292+
)
1293+
jest.advanceTimersByTime(1)
1294+
expect(root?.innerHTML ?? '').toBe(
1295+
'<svg><foreignObject height="200" x="350" width="150" y="100"></foreignObject></svg>',
1296+
)
1297+
jest.advanceTimersByTime(1)
1298+
expect(root?.innerHTML ?? '').toBe(
1299+
'<svg><foreignObject height="200" x="350" width="150" y="400"></foreignObject></svg>',
1300+
)
1301+
jest.advanceTimersByTime(1)
1302+
expect(root?.innerHTML ?? '').toBe(
1303+
'<svg><foreignObject height="200" x="350" width="450" y="400"></foreignObject></svg>',
1304+
)
1305+
jest.advanceTimersByTime(1)
1306+
expect(root?.innerHTML ?? '').toBe(
1307+
'<svg><foreignObject height="500" x="350" width="450" y="400"></foreignObject></svg>',
1308+
)
1309+
})
1310+
12421311
it('updates d', () => {
12431312
const MockComponent = () => {
12441313
const [d, setD] = useState<string | undefined>('M0,0 L0,10 Z')

packages/@react-facet/dom-fiber/src/setupHostConfig.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ const fastTypeMapSVG: Record<TypeSVG, keyof SVGElementTagNameMap> = {
11521152
'fast-path': 'path',
11531153
'fast-rect': 'rect',
11541154
'fast-svg': 'svg',
1155+
'fast-foreignObject': 'foreignObject',
11551156
'fast-use': 'use',
11561157
'fast-polyline': 'polyline',
11571158
'fast-polygon': 'polygon',
@@ -1167,6 +1168,7 @@ const fastTypeMapSVG: Record<TypeSVG, keyof SVGElementTagNameMap> = {
11671168
path: 'path',
11681169
rect: 'rect',
11691170
svg: 'svg',
1171+
foreignObject: 'foreignObject',
11701172
symbol: 'symbol',
11711173
g: 'g',
11721174
use: 'use',

packages/@react-facet/dom-fiber/src/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type TypeSVG =
2828
| 'fast-path'
2929
| 'fast-rect'
3030
| 'fast-svg'
31+
| 'fast-foreignObject'
3132
| 'fast-use'
3233
| 'fast-polyline'
3334
| 'fast-polygon'
@@ -50,6 +51,7 @@ export type TypeSVG =
5051
| 'polygon'
5152
| 'linearGradient'
5253
| 'radialGradient'
54+
| 'foreignObject'
5355
| 'stop'
5456
| 'text'
5557
| 'pattern'
@@ -290,6 +292,7 @@ export type FastPathProps = ElementProps<SVGPathElement>
290292
export type FastRectProps = ElementProps<SVGRectElement>
291293
export type FastSpanProps = ElementProps<HTMLSpanElement>
292294
export type FastSvgProps = ElementProps<SVGSVGElement>
295+
export type FastForeignOBjectProps = ElementProps<SVGForeignObjectElement>
293296
export type FastTextProps = TextProps
294297
export type FastUseProps = ElementProps<SVGUseElement>
295298
export type FastPolylineProps = ElementProps<SVGPolylineElement>
@@ -321,6 +324,7 @@ declare global {
321324
'fast-span': FastSpanProps
322325
'fast-text': FastTextProps
323326
'fast-svg': FastSvgProps
327+
'fast-foreignObject': FastForeignOBjectProps
324328
'fast-use': FastUseProps
325329
'fast-polyline': FastPolylineProps
326330
'fast-polygon': FastPolyGonProps

0 commit comments

Comments
 (0)