Skip to content

Commit f33d829

Browse files
authored
Added support for foreignObject in our custom renderer and a test alongside it (#134)
1 parent b2a9417 commit f33d829

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
@@ -128,6 +128,17 @@ describe('mount', () => {
128128
})
129129

130130
describe('for svg', () => {
131+
it('renders foreignObject and sets dimensions', () => {
132+
render(
133+
<svg>
134+
<foreignObject x="50" y="100" width="150" height="200" />
135+
</svg>,
136+
)
137+
expect(root?.innerHTML ?? '').toBe(
138+
'<svg><foreignObject height="200" x="50" width="150" y="100"></foreignObject></svg>',
139+
)
140+
})
141+
131142
it('sets the d', () => {
132143
render(<fast-path d="M0,0 L0,10 Z" />)
133144
expect(root?.innerHTML ?? '').toBe('<path d="M0,0 L0,10 Z"></path>')
@@ -517,6 +528,22 @@ describe('mount', () => {
517528
})
518529

519530
describe('for svg', () => {
531+
it('renders foreignObject and sets dimensions', () => {
532+
const xFacet = createFacet({ initialValue: '50' })
533+
const yFacet = createFacet({ initialValue: '100' })
534+
const widthFacet = createFacet({ initialValue: '150' })
535+
const heightFacet = createFacet({ initialValue: '200' })
536+
537+
render(
538+
<svg>
539+
<fast-foreignObject x={xFacet} y={yFacet} width={widthFacet} height={heightFacet} />
540+
</svg>,
541+
)
542+
expect(root?.innerHTML ?? '').toBe(
543+
'<svg><foreignObject height="200" x="50" width="150" y="100"></foreignObject></svg>',
544+
)
545+
})
546+
520547
it('sets the d', () => {
521548
const dFacet = createFacet({ initialValue: 'M0,0 L0,10 Z' })
522549

@@ -1213,6 +1240,48 @@ describe('update', () => {
12131240
})
12141241

12151242
describe('for svg', () => {
1243+
it('updates foreignObject dimensions', () => {
1244+
const MockComponent = () => {
1245+
const [xFacet, setXFacet] = useState<string | undefined>('50')
1246+
const [yFacet, setYFacet] = useState<string | undefined>('100')
1247+
const [widthFacet, setWidthFacet] = useState<string | undefined>('150')
1248+
const [heightFacet, setHeightFacet] = useState<string | undefined>('200')
1249+
1250+
useEffect(() => {
1251+
setTimeout(() => setXFacet('350'), 1)
1252+
setTimeout(() => setYFacet('400'), 2)
1253+
setTimeout(() => setWidthFacet('450'), 3)
1254+
setTimeout(() => setHeightFacet('500'), 4)
1255+
}, [])
1256+
return (
1257+
<svg>
1258+
<fast-foreignObject x={xFacet} y={yFacet} width={widthFacet} height={heightFacet} />
1259+
</svg>
1260+
)
1261+
}
1262+
1263+
render(<MockComponent />)
1264+
expect(root?.innerHTML ?? '').toBe(
1265+
'<svg><foreignObject height="200" x="50" width="150" y="100"></foreignObject></svg>',
1266+
)
1267+
jest.advanceTimersByTime(1)
1268+
expect(root?.innerHTML ?? '').toBe(
1269+
'<svg><foreignObject height="200" x="350" width="150" y="100"></foreignObject></svg>',
1270+
)
1271+
jest.advanceTimersByTime(1)
1272+
expect(root?.innerHTML ?? '').toBe(
1273+
'<svg><foreignObject height="200" x="350" width="150" y="400"></foreignObject></svg>',
1274+
)
1275+
jest.advanceTimersByTime(1)
1276+
expect(root?.innerHTML ?? '').toBe(
1277+
'<svg><foreignObject height="200" x="350" width="450" y="400"></foreignObject></svg>',
1278+
)
1279+
jest.advanceTimersByTime(1)
1280+
expect(root?.innerHTML ?? '').toBe(
1281+
'<svg><foreignObject height="500" x="350" width="450" y="400"></foreignObject></svg>',
1282+
)
1283+
})
1284+
12161285
it('updates d', () => {
12171286
const MockComponent = () => {
12181287
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
@@ -1112,6 +1112,7 @@ const fastTypeMapSVG: Record<TypeSVG, keyof SVGElementTagNameMap> = {
11121112
'fast-path': 'path',
11131113
'fast-rect': 'rect',
11141114
'fast-svg': 'svg',
1115+
'fast-foreignObject': 'foreignObject',
11151116
'fast-use': 'use',
11161117
'fast-polyline': 'polyline',
11171118
'fast-polygon': 'polygon',
@@ -1127,6 +1128,7 @@ const fastTypeMapSVG: Record<TypeSVG, keyof SVGElementTagNameMap> = {
11271128
path: 'path',
11281129
rect: 'rect',
11291130
svg: 'svg',
1131+
foreignObject: 'foreignObject',
11301132
symbol: 'symbol',
11311133
g: 'g',
11321134
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'
@@ -289,6 +291,7 @@ export type FastPathProps = ElementProps<SVGPathElement>
289291
export type FastRectProps = ElementProps<SVGRectElement>
290292
export type FastSpanProps = ElementProps<HTMLSpanElement>
291293
export type FastSvgProps = ElementProps<SVGSVGElement>
294+
export type FastForeignOBjectProps = ElementProps<SVGForeignObjectElement>
292295
export type FastTextProps = TextProps
293296
export type FastUseProps = ElementProps<SVGUseElement>
294297
export type FastPolylineProps = ElementProps<SVGPolylineElement>
@@ -320,6 +323,7 @@ declare global {
320323
'fast-span': FastSpanProps
321324
'fast-text': FastTextProps
322325
'fast-svg': FastSvgProps
326+
'fast-foreignObject': FastForeignOBjectProps
323327
'fast-use': FastUseProps
324328
'fast-polyline': FastPolylineProps
325329
'fast-polygon': FastPolyGonProps

0 commit comments

Comments
 (0)