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

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

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:

  1. import React from "react";
  2. import {
  3. Navbar,
  4. Typography,
  5. } from "@material-tailwind/react";
  6. export function CustomNavbar() {
  7. const [openNav, setOpenNav] = React.useState(false);
  8. React.useEffect(() => {
  9. window.addEventListener(
  10. "resize",
  11. () => window.innerWidth >= 960 && setOpenNav(false)
  12. );
  13. }, []);
  14. const navList = (
  15. <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">
  16. <Typography
  17. as="li"
  18. variant="small"
  19. color="blue-gray"
  20. className="p-1 font-normal"
  21. >
  22. <a href="#" className="flex items-center">
  23. Pages
  24. </a>
  25. </Typography>
  26. <Typography
  27. as="li"
  28. variant="small"
  29. color="blue-gray"
  30. className="p-1 font-normal"
  31. >
  32. <a href="#" className="flex items-center">
  33. Account
  34. </a>
  35. </Typography>
  36. <Typography
  37. as="li"
  38. variant="small"
  39. color="blue-gray"
  40. className="p-1 font-normal"
  41. >
  42. <a href="#" className="flex items-center">
  43. Blocks
  44. </a>
  45. </Typography>
  46. <Typography
  47. as="li"
  48. variant="small"
  49. color="blue-gray"
  50. className="p-1 font-normal"
  51. >
  52. <a href="#" className="flex items-center">
  53. Docs
  54. </a>
  55. </Typography>
  56. </ul>
  57. );
  58. return (
  59. <div className="">
  60. <Navbar className="sticky top-0 z-10 h-max max-w-full rounded-none bg-blue-gray-600">
  61. <div className="flex items-center justify-between text-blue-gray-900">
  62. <Typography
  63. as="a"
  64. href="#"
  65. className="mr-4 cursor-pointer py-1.5 font-medium"
  66. >
  67. Material Tailwind
  68. </Typography>
  69. <div className="flex items-center gap-4">
  70. <div className="mr-4 hidden lg:block">{navList}</div>
  71. </div>
  72. </div>
  73. </Navbar>
  74. </div>
  75. );
  76. }

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

  1. import { useEffect } from 'react';
  2. import '../styles/checkbox.css';
  3. import TodoListComponent from '../components/todo-list';
  4. import todoList from '../data/mock-data/todolist.json';
  5. import Sidebar from '../components/sidebar';
  6. import '../styles/sidebar.css';
  7. import { usePagePadding } from './function/pagePadding';
  8. import { CustomNavbar } from '../components/topnav';
  9. // redux
  10. import type { RootState } from '../redux/store';
  11. import { useSelector, useDispatch } from 'react-redux';
  12. import { onSidebarChange } from '../redux/sidebar';
  13. import { onMousePositionChange } from '../redux/mousePosition';
  14. function Todo() {
  15. // redux
  16. const pagePadding = useSelector((state: RootState) => state.pagePadding.value);
  17. const sidebar = useSelector((state: RootState) => state.sidebar.value);
  18. const mousePositionX = useSelector((state: RootState) => state.mousePosition.x);
  19. const mousePositionY = useSelector((state: RootState) => state.mousePosition.y);
  20. const dispatch = useDispatch();
  21. const toggleSidebar = () => {
  22. dispatch(onSidebarChange(!sidebar));
  23. };
  24. usePagePadding();
  25. useEffect(() => {
  26. const handleMouseMove = (event: MouseEvent) => {
  27. dispatch(onMousePositionChange({ x: event.clientX, y: event.clientY }));
  28. };
  29. window.addEventListener('mousemove', handleMouseMove);
  30. return () => {
  31. window.removeEventListener(
  32. 'mousemove',
  33. handleMouseMove
  34. );
  35. };
  36. }, []);
  37. const pageContentStyle = {
  38. paddingLeft: `${pagePadding}rem`,
  39. paddingRight: `${pagePadding}rem`,
  40. };
  41. return (
  42. <div>
  43. <CustomNavbar />
  44. <div className={`page-container ${sidebar ? 'ml-80' : ''}`}>
  45. <div className='pt-padding-heading1' style={pageContentStyle}>
  46. <h1 className="text-3xl font-bold">
  47. Task
  48. </h1>
  49. <div className='pt-padding-component-medium'></div>
  50. {todoList['todolist'].map((title, index) => (
  51. <TodoListComponent key={index} title={title} index={index}/>
  52. ))}
  53. </div>
  54. </div>
  55. </div>
  56. );
  57. }
  58. export default Todo;

App.tsx:

  1. import React from 'react';
  2. import Todo from './pages/todo';
  3. function App() {
  4. return (
  5. <div className='font-body'>
  6. <Todo />
  7. </div>
  8. );
  9. }
  10. export default App;

index.tsx:

  1. import React from 'react';
  2. import ReactDOM from 'react-dom/client';
  3. import './index.css';
  4. import App from './App';
  5. import reportWebVitals from './reportWebVitals';
  6. import { Provider } from 'react-redux';
  7. import { store } from './redux/store';
  8. // @material-tailwind/react
  9. import { ThemeProvider } from "@material-tailwind/react";
  10. const root = ReactDOM.createRoot(
  11. document.getElementById('root') as HTMLElement
  12. );
  13. root.render(
  14. <React.StrictMode>
  15. <ThemeProvider>
  16. <Provider store={store}>
  17. <App />
  18. </Provider>
  19. </ThemeProvider>
  20. </React.StrictMode>
  21. );
  22. 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:

  1. import React from &quot;react&quot;;
  2. import {
  3. Navbar,
  4. Typography,
  5. } from &quot;@material-tailwind/react&quot;;
  6. export function CustomNavbar() {
  7. const [openNav, setOpenNav] = React.useState(false);
  8. React.useEffect(() =&gt; {
  9. window.addEventListener(
  10. &quot;resize&quot;,
  11. () =&gt; window.innerWidth &gt;= 960 &amp;&amp; setOpenNav(false)
  12. );
  13. }, []);
  14. const navList = (
  15. &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;
  16. &lt;Typography
  17. as=&quot;li&quot;
  18. variant=&quot;small&quot;
  19. color=&quot;blue-gray&quot;
  20. className=&quot;p-1 font-normal&quot;
  21. &gt;
  22. &lt;a href=&quot;#&quot; className=&quot;flex items-center&quot;&gt;
  23. Pages
  24. &lt;/a&gt;
  25. &lt;/Typography&gt;
  26. &lt;Typography
  27. as=&quot;li&quot;
  28. variant=&quot;small&quot;
  29. color=&quot;blue-gray&quot;
  30. className=&quot;p-1 font-normal&quot;
  31. &gt;
  32. &lt;a href=&quot;#&quot; className=&quot;flex items-center&quot;&gt;
  33. Account
  34. &lt;/a&gt;
  35. &lt;/Typography&gt;
  36. &lt;Typography
  37. as=&quot;li&quot;
  38. variant=&quot;small&quot;
  39. color=&quot;blue-gray&quot;
  40. className=&quot;p-1 font-normal&quot;
  41. &gt;
  42. &lt;a href=&quot;#&quot; className=&quot;flex items-center&quot;&gt;
  43. Blocks
  44. &lt;/a&gt;
  45. &lt;/Typography&gt;
  46. &lt;Typography
  47. as=&quot;li&quot;
  48. variant=&quot;small&quot;
  49. color=&quot;blue-gray&quot;
  50. className=&quot;p-1 font-normal&quot;
  51. &gt;
  52. &lt;a href=&quot;#&quot; className=&quot;flex items-center&quot;&gt;
  53. Docs
  54. &lt;/a&gt;
  55. &lt;/Typography&gt;
  56. &lt;/ul&gt;
  57. );
  58. return (
  59. &lt;div className=&quot;&quot;&gt;
  60. &lt;Navbar className=&quot;sticky top-0 z-10 h-max max-w-full rounded-none bg-blue-gray-600&quot;&gt;
  61. &lt;div className=&quot;flex items-center justify-between text-blue-gray-900&quot;&gt;
  62. &lt;Typography
  63. as=&quot;a&quot;
  64. href=&quot;#&quot;
  65. className=&quot;mr-4 cursor-pointer py-1.5 font-medium&quot;
  66. &gt;
  67. Material Tailwind
  68. &lt;/Typography&gt;
  69. &lt;div className=&quot;flex items-center gap-4&quot;&gt;
  70. &lt;div className=&quot;mr-4 hidden lg:block&quot;&gt;{navList}&lt;/div&gt;
  71. &lt;/div&gt;
  72. &lt;/div&gt;
  73. &lt;/Navbar&gt;
  74. &lt;/div&gt;
  75. );
  76. }

todo.tsx: (where I call my navbar)

  1. import { useEffect } from &#39;react&#39;;
  2. import &#39;../styles/checkbox.css&#39;;
  3. import TodoListComponent from &#39;../components/todo-list&#39;;
  4. import todoList from &#39;../data/mock-data/todolist.json&#39;;
  5. import Sidebar from &#39;../components/sidebar&#39;;
  6. import &#39;../styles/sidebar.css&#39;;
  7. import { usePagePadding } from &#39;./function/pagePadding&#39;;
  8. import { CustomNavbar } from &#39;../components/topnav&#39;;
  9. // redux
  10. import type { RootState } from &#39;../redux/store&#39;;
  11. import { useSelector, useDispatch } from &#39;react-redux&#39;;
  12. import { onSidebarChange } from &#39;../redux/sidebar&#39;;
  13. import { onMousePositionChange } from &#39;../redux/mousePosition&#39;;
  14. function Todo() {
  15. // redux
  16. const pagePadding = useSelector((state: RootState) =&gt; state.pagePadding.value);
  17. const sidebar = useSelector((state: RootState) =&gt; state.sidebar.value);
  18. const mousePositionX = useSelector((state: RootState) =&gt; state.mousePosition.x);
  19. const mousePositionY = useSelector((state: RootState) =&gt; state.mousePosition.y);
  20. const dispatch = useDispatch();
  21. const toggleSidebar = () =&gt; {
  22. dispatch(onSidebarChange(!sidebar));
  23. };
  24. usePagePadding();
  25. useEffect(() =&gt; {
  26. const handleMouseMove = (event: MouseEvent) =&gt; {
  27. dispatch(onMousePositionChange({ x: event.clientX, y: event.clientY }));
  28. };
  29. window.addEventListener(&#39;mousemove&#39;, handleMouseMove);
  30. return () =&gt; {
  31. window.removeEventListener(
  32. &#39;mousemove&#39;,
  33. handleMouseMove
  34. );
  35. };
  36. }, []);
  37. const pageContentStyle = {
  38. paddingLeft: `${pagePadding}rem`,
  39. paddingRight: `${pagePadding}rem`,
  40. };
  41. return (
  42. &lt;div&gt;
  43. &lt;CustomNavbar /&gt;
  44. &lt;div className={`page-container ${sidebar ? &#39;ml-80&#39; : &#39;&#39;}`}&gt;
  45. {/* &lt;button onClick={toggleSidebar}&gt;Toggle&lt;/button&gt; */}
  46. {/* {sidebar &amp;&amp; &lt;Sidebar /&gt;} */}
  47. &lt;div className=&#39;pt-padding-heading1&#39; style={pageContentStyle}&gt;
  48. &lt;h1 className=&quot;text-3xl font-bold&quot;&gt;
  49. Task
  50. &lt;/h1&gt;
  51. &lt;div className=&#39;pt-padding-component-medium&#39;&gt;&lt;/div&gt;
  52. {todoList[&#39;todolist&#39;].map((title, index) =&gt; (
  53. &lt;TodoListComponent key={index} title={title} index={index}/&gt;
  54. ))}
  55. &lt;/div&gt;
  56. &lt;/div&gt;
  57. &lt;/div&gt;
  58. );
  59. }
  60. export default Todo;

App.tsx:

  1. import React from &#39;react&#39;;
  2. import Todo from &#39;./pages/todo&#39;;
  3. function App() {
  4. return (
  5. &lt;div className=&#39;font-body&#39;&gt;
  6. &lt;Todo /&gt;
  7. &lt;/div&gt;
  8. );
  9. }
  10. export default App;

index.tsx:

  1. import React from &#39;react&#39;;
  2. import ReactDOM from &#39;react-dom/client&#39;;
  3. import &#39;./index.css&#39;;
  4. import App from &#39;./App&#39;;
  5. import reportWebVitals from &#39;./reportWebVitals&#39;;
  6. import { Provider } from &#39;react-redux&#39;;
  7. import { store } from &#39;./redux/store&#39;;
  8. // @material-tailwind/react
  9. import { ThemeProvider } from &quot;@material-tailwind/react&quot;;
  10. const root = ReactDOM.createRoot(
  11. document.getElementById(&#39;root&#39;) as HTMLElement
  12. );
  13. root.render(
  14. &lt;React.StrictMode&gt;
  15. &lt;ThemeProvider&gt;
  16. &lt;Provider store={store}&gt;
  17. &lt;App /&gt;
  18. &lt;/Provider&gt;
  19. &lt;/ThemeProvider&gt;
  20. &lt;/React.StrictMode&gt;
  21. );
  22. reportWebVitals();

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

Please tell me which setting causing this.

答案1

得分: 0

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

  1. const theme = {
  2. navbar: {
  3. styles: {
  4. base: {
  5. navbar: {
  6. blurred: {
  7. borderWidth: '',
  8. },
  9. },
  10. },
  11. },
  12. },
  13. };
  14. <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:

  1. const theme = {
  2. navbar: {
  3. styles: {
  4. base: {
  5. navbar: {
  6. blurred: {
  7. borderWidth: &#39;&#39;,
  8. },
  9. },
  10. },
  11. },
  12. },
  13. };
  14. &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:

确定