1
0
Fork 0
freesewing/packages/react-components/src/pattern/index.mjs

87 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-06-04 23:28:43 -05:00
import { forwardRef } from 'react'
// Components that can be swizzled
import { Svg as DefaultSvg } from './svg.mjs'
import { Defs as DefaultDefs } from './defs.mjs'
import { Group as DefaultGroup } from './group.mjs'
import { Stack as DefaultStack } from './stack.mjs'
import { Part as DefaultPart } from './part.mjs'
import { Point as DefaultPoint } from './point.mjs'
import { Snippet as DefaultSnippet } from './snippet.mjs'
import { Path as DefaultPath } from './path.mjs'
import { Grid as DefaultGrid } from './grid.mjs'
import { Text as DefaultText, TextOnPath as DefaultTextOnPath } from './text.mjs'
2023-06-04 23:28:43 -05:00
import { Circle as DefaultCircle } from './circle.mjs'
/*
* Allow people to swizzle these components
*/
const defaultComponents = {
Svg: DefaultSvg,
Defs: DefaultDefs,
Group: DefaultGroup,
Stack: DefaultStack,
Part: DefaultPart,
Point: DefaultPoint,
Path: DefaultPath,
Snippet: DefaultSnippet,
Grid: DefaultGrid,
Text: DefaultText,
TextOnPath: DefaultTextOnPath,
2023-06-04 23:28:43 -05:00
Circle: DefaultCircle,
}
2023-06-04 23:28:43 -05:00
export const Pattern = forwardRef(
(
{
renderProps = false,
t = (string) => string,
components = {},
children = false,
className = 'freesewing pattern',
},
ref
) => {
if (!renderProps) return null
2023-06-04 23:28:43 -05:00
// Merge default and swizzled components
components = {
...defaultComponents,
...components,
}
2023-06-04 23:28:43 -05:00
const { Svg, Defs, Stack, Group } = components
2023-06-04 23:28:43 -05:00
const optionalProps = {}
if (className) optionalProps.className = className
2023-06-04 23:28:43 -05:00
return (
<Svg
viewBox={`0 0 ${renderProps.width} ${renderProps.height}`}
embed={renderProps.settings.embed}
{...renderProps}
{...optionalProps}
ref={ref}
>
<Defs {...renderProps} />
<style>{`:root { --pattern-scale: ${renderProps.settings.scale || 1}} ${
renderProps.svg.style
}`}</style>
<Group>
{children
? children
: Object.keys(renderProps.stacks).map((stackName) => (
<Stack
key={stackName}
stackName={stackName}
stack={renderProps.stacks[stackName]}
settings={renderProps.settings}
components={components}
t={t}
/>
))}
</Group>
</Svg>
)
}
)