英文:
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 "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: (where I call my navbar)
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' : ''}`}>
{/* <button onClick={toggleSidebar}>Toggle</button> */}
{/* {sidebar && <Sidebar />} */}
<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();
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}>
英文:
The <Navbar>
component has a 1px
white border around it by default. You can customize component styles by pass in your own theme object to <ThemeProvider>
. For example, to remove the border completely:
const theme = {
navbar: {
styles: {
base: {
navbar: {
blurred: {
borderWidth: '',
},
},
},
},
},
};
…
<ThemeProvider value={theme}>
Please find a full working example on StackBlitz.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论