portfolio/app/context/CommentFormProvider.tsx
Ryan Freeman f2eecf06d3
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m13s
Extract providers for comment component
2025-04-08 22:14:59 +01:00

24 lines
751 B
TypeScript

import {createContext, ReactNode, RefObject, useContext, useRef} from 'react'
type CommentFormContext = {
commentFormRef?: RefObject<HTMLTextAreaElement | null>
focusCommentForm: () => void
}
export const CommentFormContext = createContext<CommentFormContext | null>(null)
export const useCommentFormContext = () => useContext(CommentFormContext)
export default function CommentFormProvider({children}: { children: ReactNode }) {
const commentFormRef = useRef<HTMLTextAreaElement>(null)
const focusCommentForm = () => {
commentFormRef.current?.focus()
}
return (
<CommentFormContext.Provider value={{commentFormRef, focusCommentForm}}>
{children}
</CommentFormContext.Provider>
)
}