Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | 22x 22x 22x 22x 3995x 22x 22x 123x 44x 22x 123x 22x | import {
FC,
PropsWithChildren,
ElementType,
ComponentPropsWithoutRef,
createContext,
useContext,
useReducer,
} from 'react';
import { type TagType } from './Types';
import { TriangleArrow } from '../arrow/TriangleArrow';
export interface SymbolsElementResult<T extends object, K = string | number> {
value?: T;
parentValue?: T;
keyName?: K;
/** Index of the parent `keyName` */
keys?: K[];
}
type SymbolsElementProps<T extends TagType = 'span'> = {
as?: T;
render?: (props: SymbolsElement<T>, result: SymbolsElementResult<object>) => React.ReactNode;
'data-type'?: string;
};
export type SymbolsElement<T extends TagType = 'span'> = SymbolsElementProps<T> & ComponentPropsWithoutRef<T>;
type InitialState<T extends ElementType = 'span'> = {
Arrow?: SymbolsElement<T>;
Colon?: SymbolsElement<T>;
Quote?: SymbolsElement<T>;
ValueQuote?: SymbolsElement<T>;
BracketsRight?: SymbolsElement<T>;
BracketsLeft?: SymbolsElement<T>;
BraceRight?: SymbolsElement<T>;
BraceLeft?: SymbolsElement<T>;
};
type Dispatch = React.Dispatch<InitialState<TagType>>;
const initialState: InitialState<TagType> = {
Arrow: {
as: 'span',
className: 'w-rjv-arrow',
style: {
transform: 'rotate(0deg)',
transition: 'all 0.3s',
},
children: <TriangleArrow />,
},
Colon: {
as: 'span',
style: {
color: 'var(--w-rjv-colon-color, var(--w-rjv-color))',
marginLeft: 0,
marginRight: 2,
},
className: 'w-rjv-colon',
children: ':',
},
Quote: {
as: 'span',
style: {
color: 'var(--w-rjv-quotes-color, #236a7c)',
},
className: 'w-rjv-quotes',
children: '"',
},
ValueQuote: {
as: 'span',
style: {
color: 'var(--w-rjv-quotes-string-color, #cb4b16)',
},
className: 'w-rjv-quotes',
children: '"',
},
BracketsLeft: {
as: 'span',
style: {
color: 'var(--w-rjv-brackets-color, #236a7c)',
},
className: 'w-rjv-brackets-start',
children: '[',
},
BracketsRight: {
as: 'span',
style: {
color: 'var(--w-rjv-brackets-color, #236a7c)',
},
className: 'w-rjv-brackets-end',
children: ']',
},
BraceLeft: {
as: 'span',
style: {
color: 'var(--w-rjv-curlybraces-color, #236a7c)',
},
className: 'w-rjv-curlybraces-start',
children: '{',
},
BraceRight: {
as: 'span',
style: {
color: 'var(--w-rjv-curlybraces-color, #236a7c)',
},
className: 'w-rjv-curlybraces-end',
children: '}',
},
};
const Context = createContext<InitialState<TagType>>(initialState);
const reducer = (state: InitialState<TagType>, action: InitialState<TagType>) => ({
...state,
...action,
});
export const useSymbolsStore = () => {
return useContext(Context);
};
const DispatchSymbols = createContext<Dispatch>(() => {});
DispatchSymbols.displayName = 'JVR.DispatchSymbols';
export function useSymbols() {
return useReducer(reducer, initialState);
}
export function useSymbolsDispatch() {
return useContext(DispatchSymbols);
}
interface SymbolsProps {
initial: InitialState<TagType>;
dispatch: Dispatch;
}
export const Symbols: FC<PropsWithChildren<SymbolsProps>> = ({ initial, dispatch, children }) => {
return (
<Context.Provider value={initial}>
<DispatchSymbols.Provider value={dispatch}>{children}</DispatchSymbols.Provider>
</Context.Provider>
);
};
Symbols.displayName = 'JVR.Symbols';
|