-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract canvas prop instantiation to its own hook
Now that we have components and slots, there's a bit more complexity to how we handle things like canvas selection and rendering. So, this breaks this logic out into a standalone hook to make things more clear around what's happening. Refactor to prep for #487 Related to #467
- Loading branch information
Showing
4 changed files
with
112 additions
and
61 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@compai/css-gui': patch | ||
--- | ||
|
||
Internal refactor for canvas prop instantiation |
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,81 @@ | ||
import { createContext, ReactNode, useContext } from 'react' | ||
import { toCSSObject } from '../../lib/codegen/to-css-object' | ||
import { toReactProps } from '../../lib/codegen/to-react-props' | ||
import { useTheme } from '../providers/ThemeContext' | ||
import { useHtmlEditor } from './Provider' | ||
import { ElementPath, HtmlNode } from './types' | ||
import { cleanAttributesForCanvas, isSamePath } from './util' | ||
|
||
const DEFAULT_CANVAS_VALUE = {} | ||
const DEFAULT_ELEMENT_STYLES_IN_CANVAS = { | ||
cursor: 'default', | ||
} | ||
|
||
type CanvasProviderType = { | ||
canvas?: boolean | ||
} | ||
|
||
export function useCanvas() { | ||
const context = useContext(CanvasContext) | ||
return context | ||
} | ||
|
||
type UseCanvasPropsArguments = { | ||
path: ElementPath | ||
value: HtmlNode | ||
} | ||
|
||
export function useCanvasProps({ path, value }: UseCanvasPropsArguments) { | ||
const { canvas } = useContext(CanvasContext) | ||
const { selected, setSelected } = useHtmlEditor() | ||
const theme = useTheme() | ||
|
||
const { attributes = {}, style = {} } = value | ||
|
||
const sx = toCSSObject( | ||
{ | ||
...(canvas ? DEFAULT_ELEMENT_STYLES_IN_CANVAS : {}), | ||
...style, | ||
}, | ||
theme | ||
) | ||
|
||
if (isSamePath(path, selected) && canvas) { | ||
sx.outlineWidth = 'thin' | ||
sx.outlineStyle = 'solid' | ||
sx.outlineColor = 'primary' | ||
sx.outlineOffset = '4px' | ||
sx.userSelect = 'none' | ||
} | ||
|
||
const handleSelect = (e: MouseEvent) => { | ||
if (!canvas) { | ||
return | ||
} | ||
|
||
e.stopPropagation() | ||
setSelected(path) | ||
} | ||
|
||
const props = toReactProps({ | ||
...(canvas ? cleanAttributesForCanvas(attributes) : attributes), | ||
sx, | ||
onClick: handleSelect, | ||
}) | ||
|
||
return props | ||
} | ||
|
||
const CanvasContext = createContext<CanvasProviderType>(DEFAULT_CANVAS_VALUE) | ||
|
||
type CanvasProviderProps = CanvasProviderType & { | ||
children: ReactNode | ||
} | ||
|
||
export function CanvasProvider({ children, canvas }: CanvasProviderProps) { | ||
return ( | ||
<CanvasContext.Provider value={{ canvas }}> | ||
{children} | ||
</CanvasContext.Provider> | ||
) | ||
} |
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