PromucFlow_constructor/app/client/src/components/designSystems/appsmith/ContainerComponent.tsx

75 lines
2.3 KiB
TypeScript
Raw Normal View History

import React, { ReactNode, useRef, useEffect, RefObject } from "react";
import styled, { css } from "styled-components";
2019-09-05 17:47:50 +00:00
import { ComponentProps } from "./BaseComponent";
import { getBorderCSSShorthand, invisible } from "constants/DefaultTheme";
import { Color } from "constants/Colors";
import { generateClassName, getCanvasClassName } from "utils/generators";
const scrollContents = css`
overflow-y: auto;
`;
const StyledContainerComponent = styled.div<
ContainerComponentProps & {
ref: RefObject<HTMLDivElement>;
}
>`
2020-12-24 04:32:25 +00:00
${(props) =>
props.containerStyle !== "none"
? `
border: ${getBorderCSSShorthand(props.theme.borders[2])};
border-radius: 0;`
: ""}
height: 100%;
width: 100%;
2020-12-24 04:32:25 +00:00
background: ${(props) => props.backgroundColor};
2020-12-24 04:32:25 +00:00
${(props) => (!props.isVisible ? invisible : "")};
overflow: hidden;
2020-12-24 04:32:25 +00:00
${(props) => (props.shouldScrollContents ? scrollContents : "")}
}`;
const ContainerComponent = (props: ContainerComponentProps) => {
const containerStyle = props.containerStyle || "card";
const containerRef: RefObject<HTMLDivElement> = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!props.shouldScrollContents) {
const supportsNativeSmoothScroll =
"scrollBehavior" in document.documentElement.style;
if (supportsNativeSmoothScroll) {
containerRef.current?.scrollTo({ top: 0, behavior: "smooth" });
} else {
if (containerRef.current) {
containerRef.current.scrollTop = 0;
}
}
}
}, [props.shouldScrollContents]);
return (
<StyledContainerComponent
{...props}
ref={containerRef}
containerStyle={containerStyle}
// Before you remove: generateClassName is used for bounding the resizables within this canvas
// getCanvasClassName is used to add a scrollable parent.
className={`${
props.shouldScrollContents ? getCanvasClassName() : ""
} ${generateClassName(props.widgetId)}`}
>
{props.children}
</StyledContainerComponent>
);
};
export type ContainerStyle = "border" | "card" | "rounded-border" | "none";
export interface ContainerComponentProps extends ComponentProps {
containerStyle?: ContainerStyle;
children?: ReactNode;
className?: string;
backgroundColor?: Color;
shouldScrollContents?: boolean;
}
2019-09-09 09:08:54 +00:00
export default ContainerComponent;