Material tailwind react导航栏组件周围有额外的填充。

huangapple go评论82阅读模式
英文:

Material tailwind react Navbar component having extra padding around it

问题

navbar have extra few pixel of padding 使用 Tailwind CSS Navbar - React 来自 material tailwind react 库 (https://www.material-tailwind.com/docs/react/navbar#navbar) 时,周围有额外的内边距。我保持其他文件不变(使用 TypeScript)就像我初始化应用程序时一样。

topnav.tsx:

import React from "react";
import {
  Navbar,
  Typography,
} from "@material-tailwind/react";
 
export function CustomNavbar() {
  const [openNav, setOpenNav] = React.useState(false);
 
  React.useEffect(() => {
    window.addEventListener(
      "resize",
      () => window.innerWidth >= 960 && setOpenNav(false)
    );
  }, []);
 
  const navList = (
    <ul className="mb-4 mt-2 flex flex-col gap-2 lg:mb-0 lg:mt-0 lg:flex-row lg:items-center lg:gap-6">
      <Typography
        as="li"
        variant="small"
        color="blue-gray"
        className="p-1 font-normal"
      >
        <a href="#" className="flex items-center">
          Pages
        </a>
      </Typography>
      <Typography
        as="li"
        variant="small"
        color="blue-gray"
        className="p-1 font-normal"
      >
        <a href="#" className="flex items-center">
          Account
        </a>
      </Typography>
      <Typography
        as="li"
        variant="small"
        color="blue-gray"
        className="p-1 font-normal"
      >
        <a href="#" className="flex items-center">
          Blocks
        </a>
      </Typography>
      <Typography
        as="li"
        variant="small"
        color="blue-gray"
        className="p-1 font-normal"
      >
        <a href="#" className="flex items-center">
          Docs
        </a>
      </Typography>
    </ul>
  );
  
  return (
    <div className="">
      <Navbar className="sticky top-0 z-10 h-max max-w-full rounded-none bg-blue-gray-600">
        <div className="flex items-center justify-between text-blue-gray-900">
          <Typography
            as="a"
            href="#"
            className="mr-4 cursor-pointer py-1.5 font-medium"
          >
            Material Tailwind
          </Typography>
           <div className="flex items-center gap-4">
            <div className="mr-4 hidden lg:block">{navList}</div>
          </div>
        </div>
      </Navbar>
    </div>
  );
}

todo.tsx:(我在这里调用我的导航栏)

import { useEffect } from 'react';
import '../styles/checkbox.css'; 
import TodoListComponent from '../components/todo-list';
import todoList from '../data/mock-data/todolist.json';
import Sidebar from '../components/sidebar';
import '../styles/sidebar.css';
import { usePagePadding } from './function/pagePadding';
import { CustomNavbar } from '../components/topnav';

// redux
import type { RootState } from '../redux/store';
import { useSelector, useDispatch } from 'react-redux';
import { onSidebarChange } from '../redux/sidebar';
import { onMousePositionChange } from '../redux/mousePosition';

function Todo() {
  // redux
  const pagePadding = useSelector((state: RootState) => state.pagePadding.value);
  const sidebar = useSelector((state: RootState) => state.sidebar.value);
  const mousePositionX = useSelector((state: RootState) => state.mousePosition.x);
  const mousePositionY = useSelector((state: RootState) => state.mousePosition.y);
  const dispatch = useDispatch();

  const toggleSidebar = () => {
    dispatch(onSidebarChange(!sidebar));
  };

  usePagePadding();

  useEffect(() => {
    const handleMouseMove = (event: MouseEvent) => {
      dispatch(onMousePositionChange({ x: event.clientX, y: event.clientY }));
    };

    window.addEventListener('mousemove', handleMouseMove);

    return () => {
      window.removeEventListener(
        'mousemove',
        handleMouseMove
      );
    };
  }, []);

  const pageContentStyle = {
    paddingLeft: `${pagePadding}rem`,
    paddingRight: `${pagePadding}rem`,
  };

  return (
    <div>
      <CustomNavbar />
      <div className={`page-container ${sidebar ? 'ml-80' : ''}`}>
        <div className='pt-padding-heading1' style={pageContentStyle}>
          <h1 className="text-3xl font-bold">
            Task
          </h1>
          <div className='pt-padding-component-medium'></div>
          {todoList['todolist'].map((title, index) => (
            <TodoListComponent key={index} title={title} index={index}/>
          ))}
        </div>
      </div>
    </div>
  );
}

export default Todo;

App.tsx:

import React from 'react';
import Todo from './pages/todo';

function App() {
  return (
    <div className='font-body'>
      <Todo />
    </div>
  );
}

export default App;

index.tsx:

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { Provider } from 'react-redux';
import { store } from './redux/store';

// @material-tailwind/react
import { ThemeProvider } from "@material-tailwind/react";

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
);
root.render(
  <React.StrictMode>
    <ThemeProvider>
      <Provider store={store}>
        <App />
      </Provider>
    </ThemeProvider>
  </React.StrictMode>
);

reportWebVitals();

额外信息:我使用 Edge 进行查看。有任何建议吗?请告诉我是哪个设置导致了这个问题。

英文:

navbar have extra few pixel of padding When I use Tailwind CSS Navbar - React from material tailwind react library (https://www.material-tailwind.com/docs/react/navbar#navbar) ,there is extra padding around it. I keep other files remain unchanged (using typescript) as when I initiate the app.

topnav.tsx:

import React from &quot;react&quot;;
import {
Navbar,
Typography,
} from &quot;@material-tailwind/react&quot;;
export function CustomNavbar() {
const [openNav, setOpenNav] = React.useState(false);
React.useEffect(() =&gt; {
window.addEventListener(
&quot;resize&quot;,
() =&gt; window.innerWidth &gt;= 960 &amp;&amp; setOpenNav(false)
);
}, []);
const navList = (
&lt;ul className=&quot;mb-4 mt-2 flex flex-col gap-2 lg:mb-0 lg:mt-0 lg:flex-row lg:items-center lg:gap-6&quot;&gt;
&lt;Typography
as=&quot;li&quot;
variant=&quot;small&quot;
color=&quot;blue-gray&quot;
className=&quot;p-1 font-normal&quot;
&gt;
&lt;a href=&quot;#&quot; className=&quot;flex items-center&quot;&gt;
Pages
&lt;/a&gt;
&lt;/Typography&gt;
&lt;Typography
as=&quot;li&quot;
variant=&quot;small&quot;
color=&quot;blue-gray&quot;
className=&quot;p-1 font-normal&quot;
&gt;
&lt;a href=&quot;#&quot; className=&quot;flex items-center&quot;&gt;
Account
&lt;/a&gt;
&lt;/Typography&gt;
&lt;Typography
as=&quot;li&quot;
variant=&quot;small&quot;
color=&quot;blue-gray&quot;
className=&quot;p-1 font-normal&quot;
&gt;
&lt;a href=&quot;#&quot; className=&quot;flex items-center&quot;&gt;
Blocks
&lt;/a&gt;
&lt;/Typography&gt;
&lt;Typography
as=&quot;li&quot;
variant=&quot;small&quot;
color=&quot;blue-gray&quot;
className=&quot;p-1 font-normal&quot;
&gt;
&lt;a href=&quot;#&quot; className=&quot;flex items-center&quot;&gt;
Docs
&lt;/a&gt;
&lt;/Typography&gt;
&lt;/ul&gt;
);
return (
&lt;div className=&quot;&quot;&gt;
&lt;Navbar className=&quot;sticky top-0 z-10 h-max max-w-full rounded-none bg-blue-gray-600&quot;&gt;
&lt;div className=&quot;flex items-center justify-between text-blue-gray-900&quot;&gt;
&lt;Typography
as=&quot;a&quot;
href=&quot;#&quot;
className=&quot;mr-4 cursor-pointer py-1.5 font-medium&quot;
&gt;
Material Tailwind
&lt;/Typography&gt;
&lt;div className=&quot;flex items-center gap-4&quot;&gt;
&lt;div className=&quot;mr-4 hidden lg:block&quot;&gt;{navList}&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/Navbar&gt;
&lt;/div&gt;
);
}

todo.tsx: (where I call my navbar)

import { useEffect } from &#39;react&#39;;
import &#39;../styles/checkbox.css&#39;; 
import TodoListComponent from &#39;../components/todo-list&#39;;
import todoList from &#39;../data/mock-data/todolist.json&#39;;
import Sidebar from &#39;../components/sidebar&#39;;
import &#39;../styles/sidebar.css&#39;;
import { usePagePadding } from &#39;./function/pagePadding&#39;;
import { CustomNavbar } from &#39;../components/topnav&#39;;
// redux
import type { RootState } from &#39;../redux/store&#39;;
import { useSelector, useDispatch } from &#39;react-redux&#39;;
import { onSidebarChange } from &#39;../redux/sidebar&#39;;
import { onMousePositionChange } from &#39;../redux/mousePosition&#39;;
function Todo() {
// redux
const pagePadding = useSelector((state: RootState) =&gt; state.pagePadding.value);
const sidebar = useSelector((state: RootState) =&gt; state.sidebar.value);
const mousePositionX = useSelector((state: RootState) =&gt; state.mousePosition.x);
const mousePositionY = useSelector((state: RootState) =&gt; state.mousePosition.y);
const dispatch = useDispatch();
const toggleSidebar = () =&gt; {
dispatch(onSidebarChange(!sidebar));
};
usePagePadding();
useEffect(() =&gt; {
const handleMouseMove = (event: MouseEvent) =&gt; {
dispatch(onMousePositionChange({ x: event.clientX, y: event.clientY }));
};
window.addEventListener(&#39;mousemove&#39;, handleMouseMove);
return () =&gt; {
window.removeEventListener(
&#39;mousemove&#39;,
handleMouseMove
);
};
}, []);
const pageContentStyle = {
paddingLeft: `${pagePadding}rem`,
paddingRight: `${pagePadding}rem`,
};
return (
&lt;div&gt;
&lt;CustomNavbar /&gt;
&lt;div className={`page-container ${sidebar ? &#39;ml-80&#39; : &#39;&#39;}`}&gt;
{/* &lt;button onClick={toggleSidebar}&gt;Toggle&lt;/button&gt; */}
{/* {sidebar &amp;&amp; &lt;Sidebar /&gt;} */}
&lt;div className=&#39;pt-padding-heading1&#39; style={pageContentStyle}&gt;
&lt;h1 className=&quot;text-3xl font-bold&quot;&gt;
Task
&lt;/h1&gt;
&lt;div className=&#39;pt-padding-component-medium&#39;&gt;&lt;/div&gt;
{todoList[&#39;todolist&#39;].map((title, index) =&gt; (
&lt;TodoListComponent key={index} title={title} index={index}/&gt;
))}
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
);
}
export default Todo;

App.tsx:

import React from &#39;react&#39;;
import Todo from &#39;./pages/todo&#39;;
function App() {
return (
&lt;div className=&#39;font-body&#39;&gt;
&lt;Todo /&gt;
&lt;/div&gt;
);
}
export default App;

index.tsx:

import React from &#39;react&#39;;
import ReactDOM from &#39;react-dom/client&#39;;
import &#39;./index.css&#39;;
import App from &#39;./App&#39;;
import reportWebVitals from &#39;./reportWebVitals&#39;;
import { Provider } from &#39;react-redux&#39;;
import { store } from &#39;./redux/store&#39;;
// @material-tailwind/react
import { ThemeProvider } from &quot;@material-tailwind/react&quot;;
const root = ReactDOM.createRoot(
document.getElementById(&#39;root&#39;) as HTMLElement
);
root.render(
&lt;React.StrictMode&gt;
&lt;ThemeProvider&gt;
&lt;Provider store={store}&gt;
&lt;App /&gt;
&lt;/Provider&gt;
&lt;/ThemeProvider&gt;
&lt;/React.StrictMode&gt;
);
reportWebVitals();

extra info: I'm using edge to view.
Any suggestions?

Please tell me which setting causing this.

答案1

得分: 0

<Navbar> 组件默认有一个 1px 的白色边框。您可以通过将自己的主题对象传递给 <ThemeProvider>来自定义组件样式。例如,要完全删除边框:

const theme = {
  navbar: {
    styles: {
      base: {
        navbar: {
          blurred: {
            borderWidth: '',
          },
        },
      },
    },
  },
};

<ThemeProvider value={theme}>

请在StackBlitz上找到一个完整的工作示例

英文:

The &lt;Navbar&gt; component has a 1px white border around it by default. You can customize component styles by pass in your own theme object to &lt;ThemeProvider&gt;. For example, to remove the border completely:

const theme = {
  navbar: {
    styles: {
      base: {
        navbar: {
          blurred: {
            borderWidth: &#39;&#39;,
          },
        },
      },
    },
  },
};

&lt;ThemeProvider value={theme}&gt;

Please find a full working example on StackBlitz.

huangapple
  • 本文由 发表于 2023年7月23日 15:01:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76746994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定