Skip to content

Commit a8a179d

Browse files
committed
feat: add renderPath props.
1 parent dda2385 commit a8a179d

File tree

6 files changed

+64
-28
lines changed

6 files changed

+64
-28
lines changed

core/README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ export default function App() {
4040

4141
```jsx mdx:preview
4242
import React, { useRef, useEffect } from "react";
43-
import Signature, { getStroke, getSvgPathFromStroke, defaultOptions } from '@uiw/react-signature';
44-
45-
const point2 = [[335.0078125,77.8828125],[335.31640625,77.984375],[336.16796875,78.30078125],[337.640625,79.0546875],[339.65234375,80.44140625],[342.88671875,82.921875],[347.0625,86.28125],[353.9296875,91.546875],[358.88671875,95.125],[359.7734375,95.6875],[360.48828125,96.19921875],[360.2265625,96.42578125]];
43+
import Signature from '@uiw/react-signature';
4644

4745
const points = {
4846
"path-01": [[81.546875,38.87890625],[82.34375,37.55078125],[84.27734375,35.2265625],[87.73046875,33.2421875],[94.98046875,30.87890625],[106.29296875,28.6796875],[118.23828125,27.71875],[126.6953125,29.45703125],[134.9375,36.22265625],[141.8359375,47.13671875],[143.9765625,59.53515625],[141.8671875,76],[128.1484375,98.9609375],[106.203125,118.87109375],[95.04296875,125.8203125],[94.5859375,125.04296875],[94.7265625,122.04296875],[97.90234375,115.30859375],[109.28515625,102.3046875],[129.75390625,86.796875],[155.27734375,76.14453125],[184.99609375,71.8828125],[218.16015625,73.33984375],[249.26953125,77.8671875],[264.2421875,81.10546875],[266.51171875,82.35546875],[267.69921875,82.99609375],[267.64453125,83.08203125],[267.73046875,82.6875],[267.73828125,80.4921875],[267.83203125,75.4765625],[268.36328125,66.6484375],[268.84765625,56.4609375],[268.421875,46.88671875],[266.9296875,40.86328125],[264.90625,38.4765625],[262.87890625,37.30859375],[258.68359375,39.13671875],[251.265625,43.80078125],[242.21484375,51.79296875],[232.60546875,63.21484375],[222.6484375,78.53125],[213.01171875,94.58984375],[206.3203125,104.57421875],[201.64453125,109.59375],[198.61328125,111.87109375],[197.24609375,111.3671875],[195.421875,109.23828125],[193.87890625,104.91015625],[193.7265625,99.140625],[198.671875,89.87109375],[214.1015625,75.14453125],[243.25390625,54.8671875],[290.3671875,29.3515625],[329.31640625,10.6875],[338.9609375,6.70703125],[338.5234375,9.07421875],[336.8671875,15.4453125],[331.4296875,28.4140625],[323.234375,43.5234375],[312.70703125,57.9609375],[302.296875,66.97265625],[293.85546875,70.734375],[287.6484375,72.25],[283.45703125,70.9453125],[280.51171875,68.09375],[279.5390625,64.13671875],[280.828125,60.0234375],[284.98828125,56.40625],[294.63671875,54.62109375],[307.8984375,56.17578125],[316.7890625,62.65234375],[317.5859375,74.11328125],[309.1640625,92.50390625],[295.12109375,110.55859375],[282.93359375,123.125],[278.58203125,129.00390625],[279.46875,129.80859375],[285.25390625,129.26953125],[299.71484375,125.71875],[321.41015625,118.89453125],[349.296875,108.58203125],[372.53125,98.8671875],[387.57421875,91],[395.23046875,84.53125],[396.7421875,79.3046875],[395.31640625,75.45703125],[389.86328125,73.57421875],[374.875,77.3203125],[347.36328125,88.46875],[310.44140625,107.75],[273.14453125,130.30078125],[252.08984375,145.77734375],[246.2890625,152.421875],[245.4609375,153.8046875],[245.3671875,153.1640625],[245.78515625,148.46875],[247.5859375,136.29296875],[250.55078125,119.38671875],[253.1796875,107.87890625],[255.4765625,102.29296875],[257.40625,99.61328125],[260.58203125,99.8125],[273.9453125,105.66015625],[296.24609375,118.10546875],[326.40234375,138.03125],[355.63671875,158.3515625],[368.66796875,167.1796875],[373.015625,170.12109375],[374.7578125,170.8203125],[374.73046875,169.78125],[374.6953125,167.3203125],[374.765625,161.75],[375.6328125,154.09375],[377.94921875,146.08984375],[380.94921875,140.23046875],[383.6640625,136.3046875],[385.0546875,134.08203125],[385.19140625,133.45703125],[384.81640625,132.41015625]],
@@ -114,6 +112,33 @@ export default function App() {
114112
}
115113
```
116114

115+
## Render Path
116+
117+
```jsx mdx:preview
118+
import React, { useRef, useEffect } from "react";
119+
import Signature from '@uiw/react-signature';
120+
121+
const points = {
122+
"path-1": [[83.52734375,63.6015625],[83.22265625,64.02734375],[81.86328125,66.0390625],[78.69140625,70.90625],[72.76171875,80.44140625],[67.01171875,91.421875],[64.5390625,98.19921875],[63.83203125,101.25390625],[63.640625,102.5078125],[63.62109375,102.7109375],[63.96484375,102.22265625],[64.890625,100.87890625],[66.3671875,98.515625]],
123+
"path-2": [[116.5546875,65.8359375],[117.3125,65.8359375],[119.23046875,65.90625],[122.078125,66.39453125],[125.44140625,67.51171875],[128.33203125,69.2421875],[130.6484375,71.53515625],[131.94140625,73.6796875],[132.28125,75.65234375],[132.0625,77.5],[130.33203125,79.78125],[126.4921875,83.24609375],[120.9375,87.5234375],[114.859375,91.13671875],[108.09765625,93.66796875],[101.8359375,94.7734375],[96.26953125,94.7734375],[92.23828125,94.90625],[89.94921875,94.96484375],[88.234375,95.04296875],[88.03515625,95.08984375],[89.6015625,95.4296875],[94.75,96.640625],[107.55859375,98.640625],[123.6171875,100.09375],[135.5546875,100.734375],[141.140625,101.03515625],[142.2578125,101.08984375]]
124+
}
125+
126+
export default function App() {
127+
return (
128+
<Signature
129+
defaultPoints={points}
130+
renderPath={(path, keyName) => {
131+
if (keyName === 'path-1') {
132+
return <path d={path} fill="red" />
133+
}
134+
if (keyName === 'path-2') {
135+
return <path d={path} fill="blue" />
136+
}
137+
}}
138+
/>
139+
);
140+
}
141+
```
117142

118143
## Props
119144

core/src/Paths.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@ export const Paths = () => {
99
return (
1010
<Fragment>
1111
{Object.keys(data).map((key) => (
12-
<CreatePath key={key} data={data[key]} />
12+
<CreatePath key={key} keyName={key} data={data[key]} />
1313
))}
1414
</Fragment>
1515
);
1616
};
1717

1818
type CreatePathProps = {
1919
data: number[][];
20+
keyName: string;
2021
};
2122

22-
const CreatePath = ({ data = [] }: CreatePathProps) => {
23-
const options = useOptionStore();
23+
const CreatePath = ({ data = [], keyName }: CreatePathProps) => {
24+
const { renderPath, ...options } = useOptionStore();
2425
const stroke = getStroke(data, options);
2526
const pathData = getSvgPathFromStroke(stroke);
27+
const dom = renderPath ? renderPath(pathData, keyName, data) : null;
28+
if (dom) return dom;
2629
return (
2730
<Fragment>
2831
<path d={pathData} />

core/src/index.tsx

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,33 @@ export interface SignatureProps extends React.SVGProps<SVGSVGElement> {
1414
options?: StrokeOptions;
1515
readonly?: boolean;
1616
defaultPoints?: Record<string, number[][]>;
17+
renderPath?: (d: string, keyName: string, point: number[][]) => JSX.Element;
1718
onPointer?: (points: number[][]) => void;
1819
}
1920

20-
const Signature = forwardRef<SVGSVGElement, SignatureProps>(({ children, options, defaultPoints, ...props }, ref) => {
21-
const [state, dispatch] = useReducer(reducer, Object.assign({}, defaultPoints));
22-
const [stateOption, dispatchOption] = useReducer(reducerOption, Object.assign({ ...defaultOptions }, options));
23-
useEffect(() => dispatchOption({ ...options }), [options]);
24-
return (
25-
<PointerContext.Provider value={state}>
26-
<PointerDispatchContext.Provider value={dispatch}>
27-
<Container {...props} ref={ref}>
28-
<OptionContext.Provider value={stateOption}>
29-
<OptionDispatchContext.Provider value={dispatchOption}>
30-
<Paths />
31-
</OptionDispatchContext.Provider>
32-
</OptionContext.Provider>
33-
{children}
34-
</Container>
35-
</PointerDispatchContext.Provider>
36-
</PointerContext.Provider>
37-
);
38-
});
21+
const Signature = forwardRef<SVGSVGElement, SignatureProps>(
22+
({ children, options, renderPath, defaultPoints, ...props }, ref) => {
23+
const [state, dispatch] = useReducer(reducer, Object.assign({}, defaultPoints));
24+
const [stateOption, dispatchOption] = useReducer(
25+
reducerOption,
26+
Object.assign({ ...defaultOptions, renderPath }, options),
27+
);
28+
useEffect(() => dispatchOption({ ...options, renderPath }), [options, renderPath]);
29+
return (
30+
<PointerContext.Provider value={state}>
31+
<PointerDispatchContext.Provider value={dispatch}>
32+
<Container {...props} ref={ref}>
33+
<OptionContext.Provider value={stateOption}>
34+
<OptionDispatchContext.Provider value={dispatchOption}>
35+
<Paths />
36+
</OptionDispatchContext.Provider>
37+
</OptionContext.Provider>
38+
{children}
39+
</Container>
40+
</PointerDispatchContext.Provider>
41+
</PointerContext.Provider>
42+
);
43+
},
44+
);
3945

4046
export default Signature;

core/src/options.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createContext, useContext } from 'react';
22
import { type StrokeOptions } from 'perfect-freehand';
3+
import { SignatureProps } from './';
34

45
export const defaultOptions: InitialOptionState = {
56
size: 6,
@@ -23,7 +24,9 @@ export const OptionContext = createContext<InitialOptionState>(defaultOptions);
2324
export const OptionDispatchContext = createContext<Dispatch>(() => {});
2425

2526
type Dispatch = React.Dispatch<InitialOptionState>;
26-
type InitialOptionState = StrokeOptions;
27+
type InitialOptionState = StrokeOptions & {
28+
renderPath?: SignatureProps['renderPath'];
29+
};
2730

2831
export function reducerOption(tasks: InitialOptionState, action: InitialOptionState) {
2932
return { ...tasks, ...action };

core/src/store.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { createContext, useContext } from 'react';
2-
import { type StrokeOptions } from 'perfect-freehand';
32

43
export const initialState: InitialState = {};
54

website/src/Example.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export const ExampleSignature = () => {
154154
<button onClick={resetOption}>Reset Options</button>
155155
<button onClick={handleCopy}>Copy Options</button>
156156
<button onClick={handleSVGCopy}>Copy to SVG</button>
157-
<div>
157+
<div style={{ display: 'flex', flexWrap: 'wrap', gap: '1rem', paddingTop: '1rem' }}>
158158
<label>
159159
<div>Size: {options.size}</div>
160160
<input

0 commit comments

Comments
 (0)