import type { JSX } from 'solid-js' import { Accessor, createContext, createSignal, useContext } from 'solid-js' type WordCounter = { characters: number words: number paragraphs?: number } type EditorContextType = { isEditorPanelVisible: Accessor wordCounter: Accessor actions: { toggleEditorPanel: () => void countWords: (value: WordCounter) => void } } const EditorContext = createContext() export function useEditorContext() { return useContext(EditorContext) } export const EditorProvider = (props: { children: JSX.Element }) => { const [isEditorPanelVisible, setEditorPanelVisible] = createSignal(false) const [wordCounter, setWordCounter] = createSignal({ characters: 0, words: 0 }) const toggleEditorPanel = () => setEditorPanelVisible(!isEditorPanelVisible()) const countWords = (value) => setWordCounter(value) const actions = { toggleEditorPanel, countWords } const value: EditorContextType = { actions, isEditorPanelVisible, wordCounter } return {props.children} }