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

95 lines
2.9 KiB
TypeScript
Raw Normal View History

2019-09-05 17:47:50 +00:00
import { ComponentProps } from "./BaseComponent";
2019-11-04 10:57:19 +00:00
import { ContainerOrientation } from "../../../constants/WidgetConstants";
import styled from "../../../constants/DefaultTheme";
import React, {
createContext,
Context,
useRef,
useContext,
forwardRef,
} from "react";
2019-11-04 10:57:19 +00:00
import { FocusContext } from "../../../pages/Editor/Canvas";
import { getBorderCSSShorthand } from "../../../constants/DefaultTheme";
2019-10-31 11:26:37 +00:00
export type StyledContainerProps = ContainerProps & {
focus?: boolean;
2019-10-31 11:26:37 +00:00
imageUrl?: string;
};
export const StyledContainer = styled("div")<StyledContainerProps>`
2019-08-20 13:19:19 +00:00
display: flex;
2019-08-20 09:39:08 +00:00
flex-direction: ${props => {
2019-09-09 09:08:54 +00:00
return props.orientation === "HORIZONTAL" ? "row" : "column";
2019-03-21 12:10:32 +00:00
}};
2019-10-31 11:26:37 +00:00
background: ${props => props.imageUrl}
2019-03-19 15:21:27 +00:00
background: ${props => props.style.backgroundColor};
2019-11-06 06:35:15 +00:00
color: ${props => props.theme.colors.textDefault};
2019-03-21 12:10:32 +00:00
position: ${props => {
2019-09-09 09:08:54 +00:00
return props.style.positionType === "ABSOLUTE" ? "absolute" : "relative";
2019-03-21 12:10:32 +00:00
}};
height: 100%;
left: 0;
top: 0;
width: 100%;
2019-10-21 15:12:45 +00:00
padding: ${props => props.theme.spaces[1]}px;
&:after {
content: "${props => (props.focus ? props.widgetName : "")}";
position: absolute;
2019-10-21 15:12:45 +00:00
top: -${props => props.theme.spaces[8]}px;
font-size: ${props => props.theme.fontSizes[2]}px;
color: ${props => props.theme.colors.containerBorder};
text-align: left;
width: 100%;
}`;
/* eslint-disable react/display-name */
/* eslint-disable react/prop-types */
type Ref = HTMLDivElement;
export const Container = forwardRef<Ref, ContainerProps>((props, ref) => {
const { isFocused } = useContext(FocusContext);
const focus = isFocused === props.widgetId;
return <StyledContainer ref={ref} {...props} focus={focus} />;
});
export const ParentBoundsContext: Context<{
boundingParent?: React.RefObject<HTMLDivElement>;
}> = createContext({});
type ContainerComponentWrapperProps = ContainerStyleProps & {
isRoot?: boolean;
};
const ContainerComponentWrapper = styled.div<ContainerComponentWrapperProps>`
/* TODO(abhinav)(Issue: #107): this will changed based on the ContainerStyleProps */
border: ${props =>
!props.isRoot && getBorderCSSShorthand(props.theme.borders[2])};
box-shadow: ${props =>
!props.isRoot ? "0px 2px 4px rgba(67, 70, 74, 0.14)" : "none"};
height: 100%;
width: 100%;
`;
2019-08-29 11:22:09 +00:00
const ContainerComponent = (props: ContainerProps) => {
const container = useRef(null);
2019-10-03 11:04:11 +00:00
return (
<ParentBoundsContext.Provider value={{ boundingParent: container }}>
<Container ref={container} {...props}>
<ContainerComponentWrapper isRoot={props.isRoot}>
{props.children}
</ContainerComponentWrapper>
</Container>
</ParentBoundsContext.Provider>
);
2019-09-09 09:08:54 +00:00
};
2019-09-05 17:47:50 +00:00
export interface ContainerProps extends ComponentProps {
2019-08-29 11:22:09 +00:00
children?: JSX.Element[] | JSX.Element;
orientation?: ContainerOrientation;
isRoot?: boolean;
}
type ContainerStyleProps = {
styleName?: "border" | "card" | "rounded-border";
};
2019-09-09 09:08:54 +00:00
export default ContainerComponent;