Skip to content

Commit

Permalink
feat(sidebar):add new props and props and design revision sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
zanzebra committed Feb 22, 2022
1 parent cb8f712 commit 9ede7aa
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 61 deletions.
56 changes: 35 additions & 21 deletions src/components/Sidebar/Sidebar.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import React, { forwardRef, createContext, useState, useEffect } from 'react';
import React, { forwardRef, createContext, useState, useEffect } from "react";
import PropTypes from "prop-types";
import classNames from 'classnames';
import {SidebarContainer, SidebarInner, SidebarLayout } from "./Sidebar.style";
import classNames from "classnames";
import { SidebarContainer, SidebarInner, SidebarLayout } from "./Sidebar.style";
import { ThemeProvider } from "styled-components";
import { theme } from "./../../theme";


export const SidebarContext = createContext({
toggled: false,
handleToggleSidebar: () => {}
})
handleToggleSidebar: () => {},
});

const Sidebar = forwardRef(
(
Expand All @@ -20,59 +19,74 @@ const Sidebar = forwardRef(
backgroundColor,
breakpoint,
toggled,
minWidth,
maxWidth,
...rest
},
ref
) => {
const handleToggleSidebar = (toggled) => {
setSidebarState({ ...sidebarState, toggled: !toggled })
}
setSidebarState({ ...sidebarState, toggled: !toggled });
};

const [sidebarState, setSidebarState] = useState({
toggled,
handleToggleSidebar,
textColor,
backgroundColor,
})
breakpoint,
});

useEffect(() => {
if (toggled) {
handleToggleSidebar(!toggled);
}
}, [toggled]);

const sidebarRef = ref ? ref : React.createRef()
const sidebarRef = ref ? ref : React.createRef();

return (
<ThemeProvider theme={theme}>
<SidebarContext.Provider value={sidebarState}>
<SidebarContainer
{...rest}
ref={sidebarRef}
className={classNames('pro-sidebar', className, {
toggled: sidebarState.toggled
className={classNames("pro-sidebar", className, {
toggled: sidebarState.toggled,
})}
textColor={textColor}
backgroundColor={backgroundColor}
minWidth={minWidth}
maxWidth={maxWidth}
>
<SidebarInner>
<SidebarLayout>{children}</SidebarLayout>
</SidebarInner>
</SidebarContainer>
</SidebarContext.Provider>
</ThemeProvider>
)
);
}
)
);

Sidebar.defaultProps = {
textColor: '#fff',
backgroundColor: '#000',
}
textColor: "#ffffff",
backgroundColor: theme.colors.dark900,
breakpoint: 720,
toggled: false,
minWidth: "80px",
maxWidth: "270px",
};

Sidebar.propTypes = {
className: PropTypes.string,
children: PropTypes.node,
textColor: PropTypes.string,
backgroundColor: PropTypes.string,
}

breakpoint: PropTypes.number,
toggled: PropTypes.boolean,
};

export default Sidebar
export default Sidebar;

export { Sidebar as CDBSidebar }
export { Sidebar as CDBSidebar };
52 changes: 23 additions & 29 deletions src/components/Sidebar/Sidebar.style.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
import styled from "styled-components";


export const SidebarContainer = styled.div`
background: ${(props) => props.backgroundColor};
color: ${(props) => props.textColor};
height: 100%;
width: 270px;
min-width: 270px;
text-align: left;
transition: width, left, right, 0.3s;
position: relative;
z-index: 1009;
&.toggled{
min-width: 80px;
width: 80px;
background: ${(props) => props.backgroundColor};
color: ${(props) => props.textColor};
height: 100%;
width: ${({ maxWidth }) => maxWidth};
min-width: ${({ maxWidth }) => maxWidth};
text-align: left;
transition: width, left, right, 0.3s;
position: relative;
z-index: 1009;
&.toggled {
width: ${({ minWidth }) => minWidth};
min-width: ${({ minWidth }) => minWidth};
}
`;

export const SidebarInner = styled.div`
height: 100%;
position: relative;
z-index: 101;
height: 100%;
position: relative;
z-index: 101;
`;

export const SidebarLayout = styled.div`
height: 100%;
overflow-y: auto;
overflow-x: hidden;
position: relative;
display: flex;
flex-direction: column;
z-index: 101;
height: 100%;
overflow-y: auto;
overflow-x: hidden;
position: relative;
display: flex;
flex-direction: column;
z-index: 101;
`;


11 changes: 5 additions & 6 deletions src/components/Sidebar/SidebarContent/SidebarContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@ import { ThemeProvider } from "styled-components";
import { theme } from "./../../../theme";



const SidebarContent = forwardRef(({ children, className, ...rest }, ref) => {

const { handleToggleSidebar } = useContext(SidebarContext);
const { handleToggleSidebar, breakpoint } = useContext(SidebarContext);


const [width, setWidth] = useState(window.innerWidth);
const breakpoint = 720;
const finalbreakpoint = breakpoint || 720;



useEffect(() => {
const handleWindowResize = () => setWidth(window.innerWidth);
window.addEventListener("resize", handleWindowResize);
if (width < breakpoint) {
if (width < finalbreakpoint) {
handleToggleSidebar(false)
}

if (width > breakpoint) {
if (width > finalbreakpoint) {
handleToggleSidebar(true)
}

Expand All @@ -35,7 +34,7 @@ const SidebarContent = forwardRef(({ children, className, ...rest }, ref) => {

}

}, [handleToggleSidebar, width])
}, [handleToggleSidebar, width, finalbreakpoint])

const sidebarContentRef = ref ? ref : React.createRef();

Expand Down
1 change: 0 additions & 1 deletion src/components/Sidebar/SidebarFooter/SidebarFooter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ThemeProvider } from "styled-components";
import { theme } from "./../../../theme";



const SidebarFooter = forwardRef(({ children, className, ...rest }, ref) => {
const sidebarFooterRef = ref ? ref : React.createRef();
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const HeaderContainer = styled.div`
}
&.toggled {
justify-content: center;
.head-text {
display: none;
}
Expand Down
3 changes: 1 addition & 2 deletions src/components/Sidebar/SidebarMenu/SidebarMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import { theme } from "./../../../theme";
const SidebarMenu = forwardRef(
({ children, className, popperArrow, ...rest }, ref) => {
const menuRef = ref ? ref : React.createRef();

const { textColor, backgroundColor } = useContext(SidebarContext);


return (
<ThemeProvider theme={theme}>
<MenuNav
Expand Down
1 change: 1 addition & 0 deletions src/components/Sidebar/SidebarMenuItem/SidebarMenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const SidebarMenuItem = forwardRef(
tabIndex={0}
fontSize={textFontSize}
role="button"
toggled={toggled}
>
{icon && (
<CDBIcon
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import styled from "styled-components";
import styled, { css } from "styled-components";

export const InnerMenuItem = styled.div`
position: relative;
Expand Down Expand Up @@ -30,9 +30,14 @@ export const InnerMenuItem = styled.div`
margin-right: 23px;
}
&.toggled {
padding-left: 9px;
justify-content: center;
.item-content {
width: 0px;
display: none;
}
.suffix-wrapper {
opacity: 0;
display: none;
}
}
`;
Expand Down

0 comments on commit 9ede7aa

Please sign in to comment.