2020-03-27 09:02:11 +00:00
|
|
|
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";
|
2020-02-13 09:32:24 +00:00
|
|
|
import { invisible } from "constants/DefaultTheme";
|
2019-11-13 07:00:25 +00:00
|
|
|
import { Color } from "constants/Colors";
|
2020-03-27 09:02:11 +00:00
|
|
|
import { generateClassName, getCanvasClassName } from "utils/generators";
|
2019-11-13 07:00:25 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
const scrollContents = css`
|
|
|
|
|
overflow-y: auto;
|
|
|
|
|
position: absolute;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const StyledContainerComponent = styled.div<
|
|
|
|
|
ContainerComponentProps & {
|
|
|
|
|
ref: RefObject<HTMLDivElement>;
|
|
|
|
|
}
|
|
|
|
|
>`
|
2020-12-24 04:32:25 +00:00
|
|
|
${(props) =>
|
2019-11-13 07:00:25 +00:00
|
|
|
props.containerStyle !== "none"
|
|
|
|
|
? `
|
2020-02-13 09:32:24 +00:00
|
|
|
border: none;
|
2019-11-13 07:00:25 +00:00
|
|
|
border-radius: ${
|
|
|
|
|
props.containerStyle === "card" || props.containerStyle === "rounded-border"
|
|
|
|
|
? props.theme.radii[1]
|
|
|
|
|
: 0
|
|
|
|
|
}px;`
|
|
|
|
|
: ""}
|
2019-10-01 20:07:43 +00:00
|
|
|
height: 100%;
|
|
|
|
|
width: 100%;
|
2020-12-24 04:32:25 +00:00
|
|
|
background: ${(props) => props.backgroundColor};
|
|
|
|
|
box-shadow: ${(props) =>
|
2020-11-04 16:32:49 +00:00
|
|
|
props.containerStyle === "card" ? props.theme.shadows[2] : "none"};
|
2020-12-24 04:32:25 +00:00
|
|
|
${(props) => (!props.isVisible ? invisible : "")};
|
2020-03-27 09:02:11 +00:00
|
|
|
overflow: hidden;
|
2020-12-24 04:32:25 +00:00
|
|
|
${(props) => (props.shouldScrollContents ? scrollContents : "")}
|
2019-11-13 07:00:25 +00:00
|
|
|
}`;
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
const ContainerComponent = (props: ContainerComponentProps) => {
|
|
|
|
|
const containerStyle = props.containerStyle || "card";
|
|
|
|
|
const containerRef: RefObject<HTMLDivElement> = useRef<HTMLDivElement>(null);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!props.shouldScrollContents) {
|
2021-01-07 05:06:25 +00:00
|
|
|
const supportsNativeSmoothScroll =
|
|
|
|
|
"scrollBehavior" in document.documentElement.style;
|
|
|
|
|
if (supportsNativeSmoothScroll) {
|
|
|
|
|
containerRef.current?.scrollTo({ top: 0, behavior: "smooth" });
|
|
|
|
|
} else {
|
|
|
|
|
if (containerRef.current) {
|
|
|
|
|
containerRef.current.scrollTop = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-27 09:02:11 +00:00
|
|
|
}
|
|
|
|
|
}, [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>
|
|
|
|
|
);
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
};
|
2019-10-01 20:07:43 +00:00
|
|
|
|
2019-11-22 14:02:55 +00:00
|
|
|
export type ContainerStyle = "border" | "card" | "rounded-border" | "none";
|
2019-02-10 13:06:05 +00:00
|
|
|
|
2019-11-13 07:00:25 +00:00
|
|
|
export interface ContainerComponentProps extends ComponentProps {
|
|
|
|
|
containerStyle?: ContainerStyle;
|
|
|
|
|
children?: ReactNode;
|
|
|
|
|
className?: string;
|
|
|
|
|
backgroundColor?: Color;
|
2020-03-27 09:02:11 +00:00
|
|
|
shouldScrollContents?: boolean;
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
export default ContainerComponent;
|