parent
9a5557db03
commit
1c1de6356c
1 changed files with 28 additions and 24 deletions
|
|
@ -4,15 +4,6 @@ import ReactIcon from "@mdi/react";
|
|||
import { type Color } from "./colors";
|
||||
import * as mdiIcons from "@mdi/js";
|
||||
|
||||
export type Icon = {
|
||||
render: (s: State) => React.Node,
|
||||
size: (n: number) => Icon,
|
||||
rotate: (n: number) => Icon,
|
||||
flip: () => Icon,
|
||||
flipV: () => Icon,
|
||||
color: (c: Color | (State) => Color) => Icon
|
||||
};
|
||||
|
||||
type IconPropHelper = {
|
||||
size?: number,
|
||||
rotate?: number,
|
||||
|
|
@ -21,6 +12,27 @@ type IconPropHelper = {
|
|||
color?: Color
|
||||
};
|
||||
|
||||
export type Icon = {
|
||||
render: (s: State) => React.Node,
|
||||
size: (n: number) => Icon,
|
||||
rotate: (n: number) => Icon,
|
||||
flip: () => Icon,
|
||||
flipV: () => Icon,
|
||||
color: (c: Color | (State) => Color) => Icon,
|
||||
applyProps: (props: IconPropHelper) => Icon
|
||||
};
|
||||
|
||||
const iconChainUtils = <T> (cb: (x: T, p?: IconPropHelper) => Icon,
|
||||
p1: T, p?: IconPropHelper) => ({
|
||||
size: (n: number) => cb(p1, {...p, size: n}),
|
||||
rotate: (n: number) => cb(p1, {...p, rotate: n}),
|
||||
flip: () => cb(p1, {...p, horizontal: !p?.horizontal ?? true}),
|
||||
flipV: () => cb(p1, {...p, vertical: !p?.vertical ?? true}),
|
||||
color: (c: Color | (State) => Color) => cb(p1, {...p, color: c}),
|
||||
applyProps: (props: IconPropHelper) => cb(p1, {...p, ...props})
|
||||
}
|
||||
);
|
||||
|
||||
export const svg = (data: string, props?: IconPropHelper): Icon => {
|
||||
const propColor = ((c: Color | (State) => Color) => (state: State) => {
|
||||
if (typeof c === "function") {
|
||||
|
|
@ -37,24 +49,16 @@ export const svg = (data: string, props?: IconPropHelper): Icon => {
|
|||
color={propColor(state)}
|
||||
/>
|
||||
),
|
||||
size: (n: number) => svg(data, {...props, size: n}),
|
||||
rotate: (n: number) => svg(data, {...props, rotate: n}),
|
||||
flip: () => svg(data, {...props, horizontal: !props?.horizontal ?? true}),
|
||||
flipV: () => svg(data, {...props, vertical: !props?.vertical ?? true}),
|
||||
color: (c: Color | (State) => Color) => svg(data, {...props, color: c})
|
||||
...iconChainUtils(svg, data, props)
|
||||
};
|
||||
};
|
||||
|
||||
export const withState = (f: (s: State) => Icon): Icon => {
|
||||
return {
|
||||
render: (state) => f(state).render(state),
|
||||
size: () => withState(f),
|
||||
rotate: () => withState(f),
|
||||
flip: () => withState(f),
|
||||
flipV: () => withState(f),
|
||||
color: () => withState(f)
|
||||
};
|
||||
};
|
||||
export const withState = (f: (s: State) => Icon,
|
||||
props?: IconPropHelper): Icon => ({
|
||||
render: (state) => f(state).applyProps(props).render(state),
|
||||
...iconChainUtils(withState, f, props)
|
||||
}
|
||||
);
|
||||
|
||||
export const mdiBattery = (topic: string): Icon => withState((state) => {
|
||||
const rawval = state[topic];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue