Files
medusa-store/www/apps/api-reference/providers/loading.tsx
Shahed Nasser 4d632e7a5d docs: added tests for components in api-reference project (#14428)
* add tests (WIP)

* added test for h2

* finished adding tests

* fixes

* fixes

* fixes
2026-01-05 10:56:56 +02:00

47 lines
946 B
TypeScript

import React from "react"
import { createContext, useContext, useState } from "react"
type LoadingContextType = {
loading: boolean
removeLoading: () => void
}
const LoadingContext = createContext<LoadingContextType | null>(null)
type LoadingProviderProps = {
children: React.ReactNode
initialLoading?: boolean
}
const LoadingProvider = ({
children,
initialLoading = false,
}: LoadingProviderProps) => {
const [loading, setLoading] = useState<boolean>(initialLoading)
const removeLoading = () => setLoading(false)
return (
<LoadingContext.Provider
value={{
loading,
removeLoading,
}}
>
{children}
</LoadingContext.Provider>
)
}
export default LoadingProvider
export const useLoading = (): LoadingContextType => {
const context = useContext(LoadingContext)
if (!context) {
throw new Error("useLoading must be used inside a LoadingProvider")
}
return context
}