{"id":18569044,"url":"https://github.com/filipchalupa/shared-loading-indicator","last_synced_at":"2025-04-10T06:31:54.242Z","repository":{"id":39240256,"uuid":"466182110","full_name":"FilipChalupa/shared-loading-indicator","owner":"FilipChalupa","description":"NPM package simplifying loading state sharing in React.","archived":false,"fork":false,"pushed_at":"2024-08-30T12:31:01.000Z","size":3231,"stargazers_count":1,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-24T16:55:36.479Z","etag":null,"topics":["loading","npm-package","react","react-hooks"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/shared-loading-indicator","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FilipChalupa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-04T15:52:07.000Z","updated_at":"2024-08-30T12:31:04.000Z","dependencies_parsed_at":"2024-11-06T22:36:30.773Z","dependency_job_id":"f863c481-6818-41cb-98e5-cde025813dce","html_url":"https://github.com/FilipChalupa/shared-loading-indicator","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FilipChalupa%2Fshared-loading-indicator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FilipChalupa%2Fshared-loading-indicator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FilipChalupa%2Fshared-loading-indicator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FilipChalupa%2Fshared-loading-indicator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FilipChalupa","download_url":"https://codeload.github.com/FilipChalupa/shared-loading-indicator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248168274,"owners_count":21058804,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["loading","npm-package","react","react-hooks"],"created_at":"2024-11-06T22:31:54.676Z","updated_at":"2025-04-10T06:31:51.893Z","avatar_url":"https://github.com/FilipChalupa.png","language":"TypeScript","readme":"# Shared loading indicator [![npm](https://img.shields.io/npm/v/shared-loading-indicator.svg)](https://www.npmjs.com/package/shared-loading-indicator) ![npm type definitions](https://img.shields.io/npm/types/shared-loading-indicator.svg)\n\nSimplifies loading state sharing in React.\n\n![Demo](https://raw.githubusercontent.com/FilipChalupa/shared-loading-indicator/HEAD/screencast.gif)\n\nYou can play with it yourself here [filipchalupa.cz/shared-loading-indicator](https://filipchalupa.cz/shared-loading-indicator/).\n\n## Installation\n\n```bash\nnpm install shared-loading-indicator\n```\n\n## How to use\n\nWrap all by one `\u003cSharedLoadingIndicatorContextProvider\u003e`.\n\n```jsx\nimport { SharedLoadingIndicatorContextProvider } from 'shared-loading-indicator'\n\nexport const App =\u003e () =\u003e {\n\treturn (\n\t\t\u003cSharedLoadingIndicatorContextProvider\u003e\n\t\t\tMy app\n\t\t\u003c/SharedLoadingIndicatorContextProvider\u003e\n\t)\n}\n```\n\n### Component `\u003cSharedProgressLoadingIndicator/\u003e`\n\nPlace `SharedProgressLoadingIndicator` inside `SharedLoadingIndicatorContextProvider` to use prestyled loading indicator. See [demo](https://shared-loading-indicator.netlify.app) to change color or placement.\n\n```jsx\nimport { SharedLoadingIndicatorContextProvider, SharedProgressLoadingIndicator } from 'shared-loading-indicator'\n\nexport const App =\u003e () =\u003e {\n\treturn (\n\t\t\u003cSharedLoadingIndicatorContextProvider\u003e\n\t\t\t\u003cSharedProgressLoadingIndicator/\u003e\n\t\t\tMy app\n\t\t\u003c/SharedLoadingIndicatorContextProvider\u003e\n\t)\n}\n```\n\nYou can change the `\u003cSharedProgressLoadingIndicator\u003e` color by setting a CSS custom property on its parent named `--ProgressLoadingIndicator-color`. Or you can build your own indicator using `useSharedLoading` hook.\n\n#### CSS example\n\n```css\n:root {\n\t--ProgressLoadingIndicator-color: #ff00ff;\n}\n```\n\n![magenta indicator](https://raw.githubusercontent.com/FilipChalupa/shared-loading-indicator/HEAD/magenta-indicator.gif)\n\n### Hook `useSharedLoading()`\n\nHook `useSharedLoading` returns `true` if some component is in loading state. Use this information to show your own loading indicator (spinner, progress bar, …).\n\n```jsx\nimport { useSharedLoading } from 'shared-loading-indicator'\n\nexport const LoadingIndicator =\u003e () =\u003e {\n\tconst isLoading = useSharedLoading()\n\n\tif (!isLoading) {\n\t\treturn null\n\t}\n\n\treturn (\n\t\t\u003cdiv\u003e\n\t\t\tApp is loading something\n\t\t\u003c/div\u003e\n\t)\n}\n```\n\n#### Options\n\nYou can optionally configure `startDelay` and `minimalDuration` in milliseconds.\n\n```js\nconst isLoading = useSharedLoading({\n\tstartDelay: 300, // isLoading won't be true if all local loads get finished under 300 milliseconds\n\tminimalDuration: 1000, // isLoading will be true for at least 1000 milliseconds\n})\n```\n\n### Component `\u003cLoading/\u003e`\n\nPlace `\u003cLoading\u003e` inside `\u003cSharedLoadingIndicatorContextProvider\u003e` to signalize something is loading.\n\n```jsx\nimport { SharedLoadingIndicatorContextProvider, Loading } from 'shared-loading-indicator'\n\nexport const App =\u003e () =\u003e {\n\tconst somethingIsLoading = true // Hook this to a state\n\n\treturn (\n\t\t\u003cSharedLoadingIndicatorContextProvider\u003e\n\t\t\t{somethingIsLoading \u0026\u0026 \u003cLoading/\u003e}\n\t\t\tMy app\n\t\t\u003c/SharedLoadingIndicatorContextProvider\u003e\n\t)\n}\n```\n\n### Hook `useLocalLoading()`\n\nHook `useLocalLoading` works similarly to `useState`. It returns array with `boolean` state indicating that component is loading and set function.\n\n```jsx\nimport { useLocalLoading } from 'shared-loading-indicator'\n\nexport const MyComponent =\u003e () =\u003e {\n\tconst [isLoading, setIsLoading] = useLocalLoading()\n\n\treturn (\n\t\t\u003cdiv\u003e\n\t\t\t\u003cdiv\u003e\n\t\t\t\tIs loading: {isLoading ? 'yes' : 'no'}\n\t\t\t\u003c/div\u003e\n\t\t\t\u003cbutton onClick={() =\u003e {\n\t\t\t\tsetIsLoading(!isLoading)\n\t\t\t}}\u003e\n\t\t\u003c/div\u003e\n\t)\n}\n```\n\n```jsx\nimport { useLocalLoading } from 'shared-loading-indicator'\nimport { useEffect } from 'react'\n\nexport const LazyComponent =\u003e () =\u003e {\n\tconst [isLoading, setIsLoading] = useLocalLoading()\n\tconst [data, setData] = useState(null)\n\n\tuseEffect(() =\u003e {\n\t\tsetIsLoading(true)\n\t\tfetch('https://example.com')\n\t\t\t.then(response =\u003e response.json())\n\t\t\t.then(receivedData =\u003e {\n\t\t\t\tsetData(receivedData)\n\t\t\t})\n\t\t\t.finally(() =\u003e {\n\t\t\t\tsetIsLoading(false)\n\t\t\t})\n\t}, [])\n\n\treturn (\n\t\t\u003cdiv\u003e\n\t\t\t\u003cdiv\u003e\n\t\t\t\tIs loading: {isLoading ? 'yes' : 'no'}\n\t\t\t\u003c/div\u003e\n\t\t\t\u003cpre\u003e\n\t\t\t\t{JSON.stringify(data, null, 2)}\n\t\t\t\u003c/pre\u003e\n\t\t\u003c/div\u003e\n\t)\n}\n```\n\n### Hook `useMirrorLoading()`\n\nMirrors first argument to `useLocalMirror` under the hood.\n\n```jsx\nimport { useMirrorLoading } from 'shared-loading-indicator'\nimport { useQuery } from '@tanstack/react-query'\n\nconst Mirror = () =\u003e {\n\tconst query = useQuery([], getData)\n\tuseMirrorLoading(query.isLoading)\n\n\treturn \u003cdiv\u003e{query.data}\u003c/div\u003e\n}\n```\n\n### Suspense by `\u003cLoadingSuspense\u003e`\n\nTo capture loading state using Suspense wrap your content by `\u003cLoadingSuspense\u003e`.\n\n```jsx\nimport { LoadingSuspense } from 'shared-loading-indicator'\n\nconst LazyWithSuspense = () =\u003e {\n\treturn (\n\t\t\u003cLoadingSuspense\u003e\n\t\t\t\u003cLazyComponent /\u003e\n\t\t\u003c/LoadingSuspense\u003e\n\t)\n}\n```\n\n## Tips\n\n- [Page navigation in Next.js](tips/nextjs.md)\n- [Custom indicator with Material UI](tips/materialui.md)\n\n## Development\n\n- Install dependencies: `npm ci`\n- Run: `npm run dev`\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffilipchalupa%2Fshared-loading-indicator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffilipchalupa%2Fshared-loading-indicator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffilipchalupa%2Fshared-loading-indicator/lists"}