From 6f71305dd2a1e592986c61f148bf7f5b4bc27319 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Sat, 20 Feb 2021 11:38:04 -0500 Subject: [PATCH] Add configurable bg color to creature page. (#19) This fixes #12 by adding a background color picker to the creature page. The default background color is `#cccccc`, which I _think_ is mid-gray. --- lib/pages/creature-page.tsx | 30 ++++++++++++++++++++++-------- lib/symbol-context-widget.tsx | 4 +++- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/pages/creature-page.tsx b/lib/pages/creature-page.tsx index 0953897..507c72a 100644 --- a/lib/pages/creature-page.tsx +++ b/lib/pages/creature-page.tsx @@ -298,23 +298,26 @@ const AutoSizingSvg = React.forwardRef( ( props: { padding: number; + bgColor?: string; children: JSX.Element | JSX.Element[]; }, ref: React.ForwardedRef ) => { + const { bgColor, padding } = props; const [x, setX] = useState(0); const [y, setY] = useState(0); const [width, setWidth] = useState(1); const [height, setHeight] = useState(1); + const gRef = useRef(null); useEffect(() => { - const svgEl = ref && typeof ref === "object" && ref.current; + const svgEl = gRef.current; if (svgEl) { const bbox = svgEl.getBBox(); - setX(bbox.x - props.padding); - setY(bbox.y - props.padding); - setWidth(bbox.width + props.padding * 2); - setHeight(bbox.height + props.padding * 2); + setX(bbox.x - padding); + setY(bbox.y - padding); + setWidth(bbox.width + padding * 2); + setHeight(bbox.height + padding * 2); } }); @@ -327,7 +330,10 @@ const AutoSizingSvg = React.forwardRef( viewBox={`${x} ${y} ${width} ${height}`} ref={ref} > - {props.children} + {bgColor && ( + + )} + {props.children} ); } @@ -345,6 +351,7 @@ function getDownloadFilename(randomSeed: number | null) { export const CreaturePage: React.FC<{}> = () => { const svgRef = useRef(null); + const [bgColor, setBgColor] = useState("#cccccc"); const [randomSeed, setRandomSeed] = useState(null); const [symbolCtx, setSymbolCtx] = useState(createSvgSymbolContext()); const defaultCtx = useContext(CreatureContext); @@ -363,7 +370,14 @@ export const CreaturePage: React.FC<{}> = () => { return ( <>

Creature!

- + + + setBgColor(e.target.value)} + />{" "} +

- + {creature} diff --git a/lib/symbol-context-widget.tsx b/lib/symbol-context-widget.tsx index aa9b7d9..a6befb9 100644 --- a/lib/symbol-context-widget.tsx +++ b/lib/symbol-context-widget.tsx @@ -5,13 +5,15 @@ import { float } from "./util"; export const SymbolContextWidget: React.FC<{ ctx: SvgSymbolContext; onChange: (value: SvgSymbolContext) => void; -}> = ({ ctx, onChange }) => { + children?: any; +}> = ({ ctx, children, onChange }) => { const updateCtx = (updates: Partial) => { onChange({ ...ctx, ...updates }); }; return (

+ {children}