Skip to content

Commit

Permalink
Merge branch 'router-fix' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
matto49 committed Aug 10, 2023
2 parents c2a19c7 + 6ae587d commit 0f2c6cb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
28 changes: 22 additions & 6 deletions packages/demo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ReactDom from 'react-dom/client';
import React from 'react';
import React, { useEffect } from 'react';
import { HashRouter, useLocation, useRoutes } from 'react-router-dom';
import router from '@/routers';
import './index.scss';
Expand All @@ -9,19 +9,35 @@ const App = () => {
return useRoutes(router);
};

const AutoScrollToTop: React.FC<{ children: React.ReactElement }> = ({ children }) => {
const location = useLocation();
function useScrollToHash() {
const { hash } = useLocation();
useEffect(() => {
if (hash && hash.length) {
const element = document.querySelector(`${hash}`);
if (element) {
console.log(element.getBoundingClientRect());
window.scrollTo({ top: element.getBoundingClientRect().top - 70, behavior: 'smooth' });
}
}
}, [hash]);
}

function useAutoScrollToTop() {
useLayoutEffect(() => {
document.documentElement.scrollTo(0, 0);
}, [location.pathname]);
}

const GlobalWrap: React.FC<{ children: React.ReactElement }> = ({ children }) => {
useAutoScrollToTop();
useScrollToHash();
return children;
};

ReactDom.createRoot(document.getElementById('root') as HTMLElement).render(
<HashRouter>
<AutoScrollToTop>
<GlobalWrap>
<App />
</AutoScrollToTop>
</GlobalWrap>
</HashRouter>

);
1 change: 1 addition & 0 deletions packages/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"dependencies": {
"@mdx-js/loader": "^2.1.5",
"@mdx-js/runtime": "^1.6.22",
"babel-plugin-prismjs": "^2.1.0",
"gsap": "^3.11.5",
"pivot-design": "workspace:*",
Expand Down
15 changes: 13 additions & 2 deletions packages/demo/src/pages/component/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
// import router from '@/routers';
import './index.scss';

import { Outlet, useLocation, useNavigate } from 'react-router-dom';
import router, { ExtraRoute } from '@/routers';
import { Outlet, useNavigate } from 'react-router-dom';
import { list as DraggableList } from '../../components/Draggable/.catalog';
import { list as ButtonList } from '../../components/Button/.catalog';
import { list as InputList } from '../../components/Input/.catalog';
Expand Down Expand Up @@ -32,6 +34,14 @@ function getRouterConfig(router: ExtraRoute[], targetPath: string) {
function Index() {
const [select, setSelect] = useState('Draggable');
const navigate = useNavigate();
const location = useLocation();

useEffect(() => {
if (location.pathname === '/components') {
navigate('./button');
setSelect('button');
}
}, []);

function handleClickButton(component: string) {
setSelect(component);
Expand Down Expand Up @@ -63,6 +73,7 @@ function Index() {
{componentsList.map((item) => (
<div
className={`demo-item ${select === item.path ? 'active' : ''}`}
key={item.path}
onClick={() => handleClickButton(item.path)}
>
{item.name}
Expand Down

0 comments on commit 0f2c6cb

Please sign in to comment.