-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
541 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "visyn_core", | ||
"description": "Core repository for datavisyn applications.", | ||
"version": "11.2.0", | ||
"version": "11.3.0", | ||
"author": { | ||
"name": "datavisyn GmbH", | ||
"email": "[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from 'react'; | ||
import { StoryObj, Meta } from '@storybook/react'; | ||
import { Button, Center, Stack } from '@mantine/core'; | ||
import { useSetRef } from './useSetRef'; | ||
|
||
function SetRefTest() { | ||
const [element, setElement] = React.useState<Element>(); | ||
const { setRef } = useSetRef({ | ||
register: (el) => { | ||
setElement(el); | ||
}, | ||
cleanup: (el) => { | ||
setElement(undefined); | ||
}, | ||
}); | ||
const [visible, setVisible] = React.useState(false); | ||
|
||
const toggle = () => { | ||
setVisible((v) => !v); | ||
}; | ||
|
||
return ( | ||
<Center w={600} h={400}> | ||
<Stack> | ||
<Button onClick={toggle}>Toggle visibility</Button> | ||
|
||
{visible ? <div ref={setRef}>Current element is {element?.nodeName}</div> : <div>No element</div>} | ||
</Stack> | ||
</Center> | ||
); | ||
} | ||
|
||
const meta: Meta<typeof SetRefTest> = { | ||
component: SetRefTest, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof SetRefTest>; | ||
|
||
export const UseSetRef: Story = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as React from 'react'; | ||
import { useSyncedRef } from './useSyncedRef'; | ||
|
||
/** | ||
* Hook that provides a setRef function, passable to the ref prop of a React element. | ||
* The setRef function will register and cleanup the element with the provided callbacks. | ||
* https://legacy.reactjs.org/docs/hooks-faq.html?source=post_page-----eb7c15198780--------------------------------#how-can-i-measure-a-dom-node | ||
* https://medium.com/@teh_builder/ref-objects-inside-useeffect-hooks-eb7c15198780 | ||
*/ | ||
export function useSetRef<T extends Element>(props?: { cleanup: (lastElement: T) => void; register: (element: T) => void }) { | ||
const ref = React.useRef<T>(); | ||
|
||
const callbackRef = useSyncedRef(props); | ||
|
||
const setRef = React.useCallback( | ||
(newElement: T | null) => { | ||
if (ref.current) { | ||
callbackRef.current?.cleanup(ref.current); | ||
} | ||
|
||
if (newElement) { | ||
callbackRef.current?.register(newElement); | ||
} | ||
|
||
ref.current = newElement; | ||
}, | ||
[callbackRef], | ||
); | ||
|
||
return { | ||
// This ref is stale, do not use in useEffect dependencies | ||
ref, | ||
|
||
// This should be passed to the ref | ||
setRef, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as React from 'react'; | ||
import { LassoValue, lassoToSvgPath } from '../hooks'; | ||
|
||
export function SVGLasso({ | ||
value, | ||
strokeWidth, | ||
fill, | ||
stroke, | ||
strokeDashArray, | ||
}: { | ||
value: LassoValue; | ||
strokeWidth?: React.SVGAttributes<SVGPathElement>['strokeWidth']; | ||
fill?: React.SVGAttributes<SVGPathElement>['fill']; | ||
stroke?: React.SVGAttributes<SVGPathElement>['stroke']; | ||
strokeDashArray?: React.SVGAttributes<SVGPathElement>['strokeDasharray']; | ||
}) { | ||
return ( | ||
<path d={lassoToSvgPath(value)} fill={fill ?? 'none'} stroke={stroke ?? 'black'} strokeDasharray={strokeDashArray ?? '4'} strokeWidth={strokeWidth ?? 1} /> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './Brush'; | ||
export * from './Brushable'; | ||
export * from './SVGBrush'; | ||
export * from './SVGLasso'; |
Oops, something went wrong.