Reputation: 1745
I'm trying to replicate a collapsed Sider
without declaring collapsed={true}
purely to have the tooltip effect.
The issue is that when I wrap Menu.Item
within a Tooltip
it seems that it is no longer defined and the special trigger is not appearing
import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Layout, Menu, Icon, Tooltip } from "antd";
import { sidebar } from "./nav";
const { Header, Sider, Content } = Layout;
class SiderDemo extends React.Component {
state = {
collapsed: false
};
toggle = () => {
this.setState({
collapsed: !this.state.collapsed
});
};
render() {
return (
<Layout>
<Sider trigger={null} collapsible collapsedWidth="0" breakpoint="sm">
<div className="logo" />
<Menu theme="dark" mode="inline" defaultSelectedKeys={["1"]}>
{sidebar.map(menu => (
<Tooltip placement="right" title={menu.name}>
<Menu.Item
style={{ borderRadius: 2, width: "72px" }}
item={menu.name}
key={menu.key}
>
<Icon type={menu.icon} />
</Menu.Item>
</Tooltip>
))}
</Menu>
</Sider>
<Layout>
<Header style={{ background: "#fff", padding: 0 }}>
<Icon
className="trigger"
type={this.state.collapsed ? "menu-unfold" : "menu-fold"}
onClick={this.toggle}
/>
</Header>
<Content
style={{
margin: "24px 16px",
padding: 24,
background: "#fff",
minHeight: 280
}}
>
Content
</Content>
</Layout>
</Layout>
);
}
}
ReactDOM.render(<SiderDemo />, document.getElementById("container"));
Upvotes: 2
Views: 6952
Reputation: 53894
Currently, your code just won't work because Menu
requires its children to be only of type Menu.Item
.
Therefore, you need to add a wrapper with the same width
as the Menu.Item
.
// v remove the trigger={null} for trigger to appear
<Sider collapsible collapsedWidth="0" breakpoint="sm">
<Menu theme="dark" mode="inline" defaultSelectedKeys={['1']}>
{sidebar.map(menu => (
<Menu.Item item={menu.name} key={menu.key}>
<Tooltip placement="right" title={menu.name}>
// v Wrapper
<div style={{ width: '100%' }}>
<Icon type={menu.icon} />
</div>
</Tooltip>
</Menu.Item>
))}
</Menu>
Upvotes: 0
Reputation: 2908
Move your tooltip
inside and wrap the Menu.Item
's Icon
. I found this is the quickest way to do it.
{sidebar.map(menu => (
<Menu.Item
style={{ borderRadius: 2, width: "72px" }}
item={menu.name}
key={menu.key}
>
<Tooltip
placement="right"
title={menu.name}
className="tooltip"
>
<div className="my-icon-wrapper test">
<Icon type={menu.icon} />
</div>
</Tooltip>
</Menu.Item>
))}
css
.my-icon-wrapper {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.test {
border: 1px solid red;
}
Upvotes: 2