diff --git a/components/Collapse.js b/components/Collapse.js index e3da9dae..798e2b86 100644 --- a/components/Collapse.js +++ b/components/Collapse.js @@ -1,25 +1,60 @@ -import React, { useEffect, useRef } from 'react' +import React from 'react' +/** + * 折叠面板组件,支持水平折叠、垂直折叠 + * @param {type:['horizontal','vertical'],isOpen} props + * @returns + */ const Collapse = props => { - const collapseRef = useRef(null) + const collapseRef = React.useRef(null) + const type = props.type || 'vertical' const collapseSection = element => { const sectionHeight = element.scrollHeight + const sectionWidth = element.scrollWidth + requestAnimationFrame(function () { - element.style.height = sectionHeight + 'px' - requestAnimationFrame(function () { - element.style.height = 0 + 'px' - }) + switch (type) { + case 'horizontal': + element.style.width = sectionWidth + 'px' + requestAnimationFrame(function () { + element.style.width = 0 + 'px' + }) + break + case 'vertical': + element.style.height = sectionHeight + 'px' + requestAnimationFrame(function () { + element.style.height = 0 + 'px' + }) + } }) } + + /** + * 展开 + * @param {*} element + */ const expandSection = element => { const sectionHeight = element.scrollHeight - element.style.height = sectionHeight + 'px' - const clearTime = setTimeout(() => { - element.style.height = 'auto' - }, 400) + const sectionWidth = element.scrollWidth + let clearTime = 0 + switch (type) { + case 'horizontal': + element.style.width = sectionWidth + 'px' + clearTime = setTimeout(() => { + element.style.width = 'auto' + }, 400) + break + case 'vertical': + element.style.height = sectionHeight + 'px' + clearTime = setTimeout(() => { + element.style.height = 'auto' + }, 400) + } + clearTimeout(clearTime) } - useEffect(() => { + + React.useEffect(() => { const element = collapseRef.current if (props.isOpen) { expandSection(element) @@ -27,8 +62,9 @@ const Collapse = props => { collapseSection(element) } }, [props.isOpen]) + return ( -
+
{props.children}
) diff --git a/components/CollapseLeft.js b/components/CollapseLeft.js deleted file mode 100644 index 56d86525..00000000 --- a/components/CollapseLeft.js +++ /dev/null @@ -1,38 +0,0 @@ -import React, { useEffect, useRef } from 'react' - -const Collapse = props => { - const collapseRef = useRef(null) - const collapseSection = element => { - const sectionWidth = element.scrollWidth - requestAnimationFrame(function () { - element.style.width = sectionWidth + 'px' - requestAnimationFrame(function () { - element.style.width = 0 + 'px' - }) - }) - } - const expandSection = element => { - const sectionWidth = element.scrollWidth - element.style.width = sectionWidth + 'px' - const clearTime = setTimeout(() => { - element.style.width = 'auto' - }, 400) - clearTimeout(clearTime) - } - useEffect(() => { - const element = collapseRef.current - if (props.isOpen) { - expandSection(element) - } else { - collapseSection(element) - } - }, [props.isOpen]) - return ( -
- {props.children} -
- ) -} -Collapse.defaultProps = { isOpen: false } - -export default Collapse diff --git a/themes/fukasawa/components/TopNav.js b/themes/fukasawa/components/TopNav.js index ae900604..7653c8da 100644 --- a/themes/fukasawa/components/TopNav.js +++ b/themes/fukasawa/components/TopNav.js @@ -20,7 +20,7 @@ const TopNav = props => { {/* 导航栏 */} - +
diff --git a/themes/medium/components/TopNavBar.js b/themes/medium/components/TopNavBar.js index 1ade1b77..5b66bcab 100644 --- a/themes/medium/components/TopNavBar.js +++ b/themes/medium/components/TopNavBar.js @@ -19,7 +19,7 @@ export default function TopNavBar(props) { } return
- +
diff --git a/themes/next/components/TopNav.js b/themes/next/components/TopNav.js index c56daf17..f8b815c1 100644 --- a/themes/next/components/TopNav.js +++ b/themes/next/components/TopNav.js @@ -109,7 +109,7 @@ const TopNav = (props) => {
- +