-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a generic Loading component for use in Admin
`Loading` handles the positioning of the loading indicator automatically, depending on the set `behaviour` prop.
- Loading branch information
1 parent
4312269
commit 07d921d
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@comet/admin": minor | ||
--- | ||
|
||
Add a generic Loading component for use in Admin | ||
|
||
`Loading` handles the positioning of the loading indicator automatically, depending on the set `behaviour` prop. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { CircularProgress, CircularProgressProps } from "@mui/material"; | ||
import { css, styled } from "@mui/material/styles"; | ||
import React from "react"; | ||
|
||
export interface LoadingProps extends CircularProgressProps { | ||
behavior?: "auto" | "fillParent" | "fillParentAbsolute" | "fillPageHeight"; | ||
} | ||
|
||
export const Loading = ({ behavior = "auto", ...circularProgressProps }: LoadingProps) => { | ||
const rootRef = React.useRef<HTMLDivElement>(null); | ||
const offsetTop = rootRef.current?.offsetTop || 0; | ||
|
||
return ( | ||
<Root ref={rootRef} style={{ "--comet-admin-loading-offset-top": `${offsetTop}px` } as React.CSSProperties} behavior={behavior}> | ||
<LoadingContainer behavior={behavior}> | ||
<CircularProgress {...circularProgressProps} /> | ||
</LoadingContainer> | ||
</Root> | ||
); | ||
}; | ||
|
||
const Root = styled("div")<Required<Pick<LoadingProps, "behavior">>>` | ||
position: relative; | ||
${({ behavior }) => { | ||
if (behavior === "fillParent") { | ||
return css` | ||
height: 100%; | ||
flex-grow: 1; | ||
`; | ||
} | ||
if (behavior === "fillParentAbsolute") { | ||
return css` | ||
position: absolute; | ||
inset: 0; | ||
`; | ||
} | ||
if (behavior === "fillPageHeight") { | ||
return css` | ||
height: calc(100vh - var(--comet-admin-loading-offset-top)); | ||
`; | ||
} | ||
}} | ||
`; | ||
|
||
const LoadingContainer = styled("div")<Required<Pick<LoadingProps, "behavior">>>` | ||
text-align: center; | ||
${({ behavior }) => | ||
behavior !== "auto" && | ||
css` | ||
position: absolute; | ||
inset: 0; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
`} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters