-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement scrollable form components
Signed-off-by: Mason Hu <[email protected]>
- Loading branch information
Showing
26 changed files
with
226 additions
and
134 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,41 @@ | ||
import React, { DependencyList, FC, ReactNode, useEffect, useRef } from "react"; | ||
import useEventListener from "@use-it/event-listener"; | ||
import { getAbsoluteHeightBelow, getParentsBottomSpacing } from "util/helpers"; | ||
|
||
interface Props { | ||
children: ReactNode; | ||
dependencies: DependencyList; | ||
belowId?: string; | ||
} | ||
|
||
const ScrollableContainer: FC<Props> = ({ | ||
dependencies, | ||
children, | ||
belowId = "", | ||
}) => { | ||
const ref = useRef<HTMLDivElement>(null); | ||
|
||
const updateChildContainerHeight = () => { | ||
const childContainer = ref.current?.children[0]; | ||
if (!childContainer) { | ||
return; | ||
} | ||
const above = childContainer.getBoundingClientRect().top + 1; | ||
const below = getAbsoluteHeightBelow(belowId); | ||
const parentsBottomSpacing = getParentsBottomSpacing(childContainer); | ||
const offset = Math.ceil(above + below + parentsBottomSpacing); | ||
const style = `height: calc(100vh - ${offset}px); min-height: calc(100vh - ${offset}px)`; | ||
childContainer.setAttribute("style", style); | ||
}; | ||
|
||
useEventListener("resize", updateChildContainerHeight); | ||
useEffect(updateChildContainerHeight, [...dependencies, ref]); | ||
|
||
return ( | ||
<div ref={ref} className="scrollable-container"> | ||
<div className="content-details">{children}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ScrollableContainer; |
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,21 @@ | ||
import React, { FC, ReactNode } from "react"; | ||
import ScrollableContainer from "./ScrollableContainer"; | ||
import { useNotify } from "@canonical/react-components"; | ||
|
||
interface Props { | ||
children: ReactNode; | ||
} | ||
|
||
const ScrollableForm: FC<Props> = ({ children }) => { | ||
const notify = useNotify(); | ||
return ( | ||
<ScrollableContainer | ||
dependencies={[notify.notification]} | ||
belowId="form-footer" | ||
> | ||
{children} | ||
</ScrollableContainer> | ||
); | ||
}; | ||
|
||
export default ScrollableForm; |
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
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
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
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
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
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
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
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
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
Oops, something went wrong.