Balloon 气泡提示

如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。

安装方法

  1. 在命令行中执行以下命令npm install @icedesign/base@latest -S

开发指南

气泡组件

何时使用

  • 当用户与被说明对象(文字,图片,输入框等)发生交互行为的action开始时, 即刻跟随动作出现一种辅助或帮助的提示信息。

  • 其中Balloon.Tooltip是简化版本,主要用于hover时显示简单文案。

使用注意

  • 对于trigger是自定义的React Component的情况,自定义的React Component 需要透传onMouseEnter/onMouseLeave/onClick 事件。

API

气泡提示

参数说明类型默认值
prefix样式类名的品牌前缀String'next-'
className自定义类名String-
style自定义内敛样式Object-
children浮层的内容any-
type样式类型可选值:'normal', 'primary'Enum'normal'
visible弹层当前显示的状态Boolean-
defaultVisible弹层默认显示的状态Booleanfalse
onVisibleChange弹层在显示和隐藏触发的事件签名:Function(visible: Boolean) => void参数:visible: {Boolean} 弹层是否隐藏和显示Function-
alignment弹出层对齐方式可选值:'normal'(普通对齐 箭头居中)'edge'(边缘对齐 箭头可在trigger的边缘)Enum'normal'
closable是否显示关闭按钮Booleantrue
align弹出层位置可选值:'t'(上)'r'(右)'b'(下)'l'(左)'tl'(上左)'tr'(上右)'bl'(下左)'br'(下右)'lt'(左上)'lb'(左下)'rt'(右上)'rb'(右下)Enum'b'
offset弹层相对于trigger的定位的微调Array0, 0
trigger触发元素any<span></span>
triggerType触发行为可选值:'hover'(鼠标悬浮)'focus'(获取到焦点)'click'(鼠标点击)Enum'hover'
onClose任何visible为false时会触发的事件签名:Function() => voidFunction() => { }
onCloseClick点击关闭按钮的click事件签名:Function() => voidFunction() => { }
needAdjust是否进行自动位置调整Booleanfalse
delay弹层在触发以后的延时显示Number-
afterClose浮层关闭后触发的事件, 如果有动画,则在动画结束后触发签名:Function() => voidFunction() => { }
shouldUpdatePosition强制更新定位信息Boolean-
autoFocus弹层出现后是否自动focus到内部第一个元素Booleanfalse
safeNode安全节点:对于triggetType为click的浮层,会在点击除了浮层外的其它区域时关闭浮层.safeNode用于添加不触发关闭的节点, 值可以是dom节点的id或者是节点的dom对象Stringundefined
safeId用来指定safeNode节点的id,和safeNode配合使用Stringnull
animation配置动画的播放方式Object/Boolean{ in: 'zoomIn', out: 'zoomOut' }
cache弹层的dom节点关闭时是否删除Booleanfalse
container指定浮层渲染的父节点, 可以为节点id的字符串,也可以返回节点的函数。String/Function-

Balloon.Tooltip

参数说明类型默认值
prefix样式类名的品牌前缀String-
className自定义类名String-
style自定义内敛样式Object-
align弹出层位置可选值:'t'(上)'r'(右)'b'(下)'l'(左)'tl'(上左)'tr'(上右)'bl'(下左)'br'(下右)'lt'(左上)'lb'(左下)'rt'(右上)'rb'(右下)Enum'b'
texttooltip 内部内容文案ReactNode''
trigger触发元素any<span></span>

代码示例

边缘对齐设置

位置有十二个方向。

Balloon 气泡提示 - 图1

查看源码在线预览

  1. import { Button, Balloon } from "@icedesign/base";
  2. const top = (
  3. <Button id="top" style={{ margin: "5px" }} className="btrigger">
  4. 上边
  5. </Button>
  6. );
  7. const right = (
  8. <Button id="right" style={{ margin: "5px" }} className="btrigger">
  9. 右边
  10. </Button>
  11. );
  12. const bottom = (
  13. <Button id="bottom" style={{ margin: "5px" }} className="btrigger">
  14. 下边
  15. </Button>
  16. );
  17. const left = (
  18. <Button id="left" style={{ margin: "5px" }} className="btrigger">
  19. 左边
  20. </Button>
  21. );
  22. const topLeft = (
  23. <Button id="topLeft" style={{ margin: "5px" }} className="btrigger">
  24. 上左
  25. </Button>
  26. );
  27. const topRight = (
  28. <Button id="topRight" style={{ margin: "5px" }} className="btrigger">
  29. 上右
  30. </Button>
  31. );
  32. const rightTop = (
  33. <Button id="rightTop" style={{ margin: "5px" }} className="btrigger">
  34. 右上
  35. </Button>
  36. );
  37. const rightBottom = (
  38. <Button id="rightBottom" style={{ margin: "5px" }} className="btrigger">
  39. 右下
  40. </Button>
  41. );
  42. const bottomLeft = (
  43. <Button id="bottomLeft" style={{ margin: "5px" }} className="btrigger">
  44. 下左
  45. </Button>
  46. );
  47. const bottomRight = (
  48. <Button id="bottomRight" style={{ margin: "5px" }} className="btrigger">
  49. 下右
  50. </Button>
  51. );
  52. const leftTop = (
  53. <Button id="leftTop" style={{ margin: "5px" }} className="btrigger">
  54. 左上
  55. </Button>
  56. );
  57. const leftBottom = (
  58. <Button id="leftBottom" style={{ margin: "5px" }} className="btrigger">
  59. 左下
  60. </Button>
  61. );
  62. const Content = () => (
  63. <div>
  64. <h4 style={{ marginTop: 0 }}>balloon title</h4>
  65. <hr />
  66. <p>balloon content</p>
  67. </div>
  68. );
  69. const App = () => (
  70. <div style={{ paddingLeft: 320, paddingTop: 100 }}>
  71. <div style={{ marginLeft: 75 }}>
  72. <Balloon
  73. trigger={topLeft}
  74. align="tl"
  75. alignment="edge"
  76. triggerType="click"
  77. style={{ width: 300 }}
  78. >
  79. <Content />
  80. </Balloon>
  81. <Balloon
  82. trigger={top}
  83. align="t"
  84. alignment="edge"
  85. triggerType="click"
  86. style={{ width: 300 }}
  87. >
  88. <Content />
  89. </Balloon>
  90. <Balloon
  91. trigger={topRight}
  92. align="tr"
  93. alignment="edge"
  94. triggerType="click"
  95. style={{ width: 300 }}
  96. >
  97. <Content />
  98. </Balloon>
  99. </div>
  100. <div style={{ width: 80, float: "left" }}>
  101. <Balloon
  102. trigger={leftTop}
  103. align="lt"
  104. alignment="edge"
  105. triggerType="click"
  106. style={{ width: 300 }}
  107. >
  108. <Content />
  109. </Balloon>
  110. <Balloon
  111. trigger={left}
  112. align="l"
  113. alignment="edge"
  114. triggerType="click"
  115. style={{ width: 300 }}
  116. >
  117. <Content />
  118. </Balloon>
  119. <Balloon
  120. trigger={leftBottom}
  121. align="lb"
  122. alignment="edge"
  123. triggerType="click"
  124. style={{ width: 300 }}
  125. >
  126. <Content />
  127. </Balloon>
  128. </div>
  129. <div style={{ width: 80, marginLeft: 290 }}>
  130. <Balloon
  131. trigger={rightTop}
  132. align="rt"
  133. alignment="edge"
  134. triggerType="click"
  135. style={{ width: 300 }}
  136. >
  137. <Content />
  138. </Balloon>
  139. <Balloon
  140. trigger={right}
  141. align="r"
  142. alignment="edge"
  143. triggerType="click"
  144. style={{ width: 300 }}
  145. >
  146. <Content />
  147. </Balloon>
  148. <Balloon
  149. trigger={rightBottom}
  150. align="rb"
  151. alignment="edge"
  152. triggerType="click"
  153. style={{ width: 300 }}
  154. >
  155. <Content />
  156. </Balloon>
  157. </div>
  158. <div style={{ marginLeft: 80, clear: "both" }}>
  159. <Balloon
  160. trigger={bottomLeft}
  161. align="bl"
  162. alignment="edge"
  163. triggerType="click"
  164. style={{ width: 300 }}
  165. >
  166. <Content />
  167. </Balloon>
  168. <Balloon
  169. trigger={bottom}
  170. align="b"
  171. alignment="edge"
  172. triggerType="click"
  173. style={{ width: 300 }}
  174. >
  175. <Content />
  176. </Balloon>
  177. <Balloon
  178. trigger={bottomRight}
  179. align="br"
  180. alignment="edge"
  181. triggerType="click"
  182. style={{ width: 300 }}
  183. >
  184. <Content />
  185. </Balloon>
  186. </div>
  187. </div>
  188. );
  189. ReactDOM.render(<App />, mountNode);

基本

最简单的用法。

Balloon 气泡提示 - 图2

查看源码在线预览

  1. import { Button, Balloon } from "@icedesign/base";
  2. const defaultTrigger = (
  3. <Button className="btrigger" style={{ margin: "5px" }}>
  4. 默认样式
  5. </Button>
  6. );
  7. const primary = (
  8. <Button className="btrigger" style={{ margin: "5px" }}>
  9. 强调样式
  10. </Button>
  11. );
  12. const Demo = () => (
  13. <div className="container">
  14. <Balloon trigger={defaultTrigger} closable={false}>
  15. default
  16. </Balloon>
  17. <Balloon type="primary" trigger={primary} triggerType="click">
  18. primary
  19. </Balloon>
  20. </div>
  21. );
  22. ReactDOM.render(<Demo />, mountNode);

从浮层内关闭, 事件回调

使用 visible ,属性控制浮层显示, 使 balloon 变为受限组件。

Balloon 气泡提示 - 图3

查看源码在线预览

  1. import { Button, Balloon } from "@icedesign/base";
  2. class App extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = {
  6. visible: false
  7. };
  8. }
  9. hide() {
  10. this.setState({
  11. visible: false
  12. });
  13. }
  14. // onVisibleChange事件会在所有visible属性被改变的时候触发;
  15. // 比如对于click类型,会在点击button的时候触发和点击空白区域的时候触发;
  16. // 对于hover类型,会在mouseentter,mouseleave的时候触发;
  17. handleVisibleChange(visible) {
  18. this.setState({ visible });
  19. }
  20. onClose() {
  21. console.log("onClose doing!");
  22. }
  23. afterClose() {
  24. console.log("afterClose doing!");
  25. }
  26. render() {
  27. const visibleTrigger = (
  28. <Button type="primary" style={{ margin: "5px" }}>
  29. 点击弹出卡片
  30. </Button>
  31. );
  32. const clickTrigger = (
  33. <Button type="primary" style={{ margin: "5px" }}>
  34. hover弹出卡片
  35. </Button>
  36. );
  37. const content = (
  38. <div>
  39. 点击按钮操作<br />
  40. <a style={{ right: 0 }} id="confirmBtn" onClick={this.hide.bind(this)}>
  41. 确认
  42. </a>
  43. <a
  44. style={{ marginLeft: "4px" }}
  45. id="cancelBtn"
  46. onClick={this.hide.bind(this)}
  47. >
  48. 关闭
  49. </a>
  50. </div>
  51. );
  52. return (
  53. <div>
  54. <Balloon
  55. trigger={visibleTrigger}
  56. triggerType="click"
  57. visible={this.state.visible}
  58. onVisibleChange={this.handleVisibleChange.bind(this)}
  59. >
  60. {content}
  61. </Balloon>
  62. <Balloon
  63. trigger={clickTrigger}
  64. triggerType="hover"
  65. onClose={this.onClose.bind(this)}
  66. afterClose={this.afterClose.bind(this)}
  67. >
  68. 内容区域
  69. </Balloon>
  70. </div>
  71. );
  72. }
  73. }
  74. ReactDOM.render(<App />, mountNode);

嵌套浮层问题

浮层中如果又有浮层,比如在Balloon中有DatePicker的浮层,需要用safeNode解决datePicker选择时,balloon浮层关闭的问题.

Balloon 气泡提示 - 图4

查看源码在线预览

  1. import { Button, Balloon, DatePicker } from "@icedesign/base";
  2. const primary = <Button className="btrigger">primary</Button>;
  3. const innerButton = <Button className="btrigger">innerButton</Button>;
  4. const App = () => (
  5. <div className="container nested">
  6. <Balloon
  7. type="primary"
  8. trigger={primary}
  9. closable={false}
  10. triggerType="click"
  11. >
  12. <DatePicker
  13. defaultValue="2016-10-10"
  14. container={trigger => trigger.parentNode}
  15. />
  16. </Balloon>
  17. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  18. <Balloon
  19. type="primary"
  20. trigger={innerButton}
  21. closable={false}
  22. triggerType="click"
  23. >
  24. <Balloon
  25. trigger={<Button type="primary">please click</Button>}
  26. container={trigger => trigger.parentNode}
  27. triggerType="click"
  28. >
  29. nesting balloon content
  30. </Balloon>
  31. </Balloon>
  32. </div>
  33. );
  34. ReactDOM.render(<App />, mountNode);
  1. .container.nested {
  2. margin-left: 100px;
  3. margin-bottom: 50px;
  4. }

close按钮事件,手动控制visible

使用 visible,属性控制浮层显示, 使balloon变为受限组件。

Balloon 气泡提示 - 图5

查看源码在线预览

  1. import { Button, Balloon } from "@icedesign/base";
  2. class App extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = {
  6. visible: false
  7. };
  8. }
  9. // 一切改变visible为false的行为都会触发此事件
  10. onClose() {
  11. console.log("onClose doing!");
  12. }
  13. // close按钮的click事件
  14. onCloseClick() {
  15. this.setState({ visible: false });
  16. }
  17. onClick() {
  18. this.setState({ visible: true });
  19. }
  20. render() {
  21. const visibleTrigger = (
  22. <Button onClick={this.onClick.bind(this)} type="primary">
  23. 点击弹出卡片
  24. </Button>
  25. );
  26. const content = <div>content</div>;
  27. return (
  28. <div style={{ marginBottom: "100px" }}>
  29. <Balloon
  30. trigger={visibleTrigger}
  31. triggerType="click"
  32. visible={this.state.visible}
  33. onCloseClick={this.onCloseClick.bind(this)}
  34. onClose={this.onClose.bind(this)}
  35. >
  36. {content}
  37. </Balloon>
  38. </div>
  39. );
  40. }
  41. }
  42. ReactDOM.render(<App />, mountNode);

位置

位置有十二个方向。

Balloon 气泡提示 - 图6

查看源码在线预览

  1. import { Button, Balloon } from "@icedesign/base";
  2. const top = (
  3. <Button id="top" style={{ margin: "5px" }} className="btrigger">
  4. 上边
  5. </Button>
  6. );
  7. const right = (
  8. <Button id="right" style={{ margin: "5px" }} className="btrigger">
  9. 右边
  10. </Button>
  11. );
  12. const bottom = (
  13. <Button id="bottom" style={{ margin: "5px" }} className="btrigger">
  14. 下边
  15. </Button>
  16. );
  17. const left = (
  18. <Button id="left" style={{ margin: "5px" }} className="btrigger">
  19. 左边
  20. </Button>
  21. );
  22. const topLeft = (
  23. <Button id="topLeft" style={{ margin: "5px" }} className="btrigger">
  24. 上左
  25. </Button>
  26. );
  27. const topRight = (
  28. <Button id="topRight" style={{ margin: "5px" }} className="btrigger">
  29. 上右
  30. </Button>
  31. );
  32. const rightTop = (
  33. <Button id="rightTop" style={{ margin: "5px" }} className="btrigger">
  34. 右上
  35. </Button>
  36. );
  37. const rightBottom = (
  38. <Button id="rightBottom" style={{ margin: "5px" }} className="btrigger">
  39. 右下
  40. </Button>
  41. );
  42. const bottomLeft = (
  43. <Button id="bottomLeft" style={{ margin: "5px" }} className="btrigger">
  44. 下左
  45. </Button>
  46. );
  47. const bottomRight = (
  48. <Button id="bottomRight" style={{ margin: "5px" }} className="btrigger">
  49. 下右
  50. </Button>
  51. );
  52. const leftTop = (
  53. <Button id="leftTop" style={{ margin: "5px" }} className="btrigger">
  54. 左上
  55. </Button>
  56. );
  57. const leftBottom = (
  58. <Button id="leftBottom" style={{ margin: "5px" }} className="btrigger">
  59. 左下
  60. </Button>
  61. );
  62. const Content = () => (
  63. <div>
  64. <h4 style={{ marginTop: 0 }}>balloon title</h4>
  65. <hr />
  66. <p>balloon content</p>
  67. </div>
  68. );
  69. const App = () => (
  70. <div style={{ paddingLeft: 320, paddingTop: 100 }}>
  71. <div style={{ marginLeft: 75 }}>
  72. <Balloon
  73. trigger={topLeft}
  74. align="tl"
  75. triggerType="click"
  76. style={{ width: 300 }}
  77. >
  78. <Content />
  79. </Balloon>
  80. <Balloon
  81. trigger={top}
  82. align="t"
  83. triggerType="click"
  84. style={{ width: 300 }}
  85. >
  86. <Content />
  87. </Balloon>
  88. <Balloon
  89. trigger={topRight}
  90. align="tr"
  91. triggerType="click"
  92. style={{ width: 300 }}
  93. >
  94. <Content />
  95. </Balloon>
  96. </div>
  97. <div style={{ width: 80, float: "left" }}>
  98. <Balloon
  99. trigger={leftTop}
  100. align="lt"
  101. triggerType="click"
  102. style={{ width: 300 }}
  103. >
  104. <Content />
  105. </Balloon>
  106. <Balloon
  107. trigger={left}
  108. align="l"
  109. triggerType="click"
  110. style={{ width: 300 }}
  111. >
  112. <Content />
  113. </Balloon>
  114. <Balloon
  115. trigger={leftBottom}
  116. align="lb"
  117. triggerType="click"
  118. style={{ width: 300 }}
  119. >
  120. <Content />
  121. </Balloon>
  122. </div>
  123. <div style={{ width: 80, marginLeft: 290 }}>
  124. <Balloon
  125. trigger={rightTop}
  126. align="rt"
  127. triggerType="click"
  128. style={{ width: 300 }}
  129. >
  130. <Content />
  131. </Balloon>
  132. <Balloon
  133. trigger={right}
  134. align="r"
  135. triggerType="click"
  136. style={{ width: 300 }}
  137. >
  138. <Content />
  139. </Balloon>
  140. <Balloon
  141. trigger={rightBottom}
  142. align="rb"
  143. triggerType="click"
  144. style={{ width: 300 }}
  145. >
  146. <Content />
  147. </Balloon>
  148. </div>
  149. <div style={{ marginLeft: 80, clear: "both" }}>
  150. <Balloon
  151. trigger={bottomLeft}
  152. align="bl"
  153. triggerType="click"
  154. style={{ width: 300 }}
  155. >
  156. <Content />
  157. </Balloon>
  158. <Balloon
  159. trigger={bottom}
  160. align="b"
  161. triggerType="click"
  162. style={{ width: 300 }}
  163. >
  164. <Content />
  165. </Balloon>
  166. <Balloon
  167. trigger={bottomRight}
  168. align="br"
  169. triggerType="click"
  170. style={{ width: 300 }}
  171. >
  172. <Content />
  173. </Balloon>
  174. </div>
  175. </div>
  176. );
  177. ReactDOM.render(<App />, mountNode);

tooltip

简化的popup, 只能设置align, trigger和text。tooltip里的只能是文本, 通过text设置, 触发条件是hover.

Balloon 气泡提示 - 图7

查看源码在线预览

  1. import { Button, Balloon } from "@icedesign/base";
  2. const Tooltip = Balloon.Tooltip;
  3. const top = (
  4. <Button style={{ margin: "5px" }} id="top" className="btrigger">
  5. 上边
  6. </Button>
  7. );
  8. const right = (
  9. <Button style={{ margin: "5px" }} id="right" className="btrigger">
  10. 右边
  11. </Button>
  12. );
  13. const bottom = (
  14. <Button style={{ margin: "5px" }} id="bottom" className="btrigger">
  15. 下边
  16. </Button>
  17. );
  18. const left = (
  19. <Button style={{ margin: "5px" }} id="left" className="btrigger">
  20. 左边
  21. </Button>
  22. );
  23. const topLeft = (
  24. <Button style={{ margin: "5px" }} id="topLeft" className="btrigger">
  25. 上左
  26. </Button>
  27. );
  28. const topRight = (
  29. <Button style={{ margin: "5px" }} id="topRight" className="btrigger">
  30. 上右
  31. </Button>
  32. );
  33. const rightTop = (
  34. <Button style={{ margin: "5px" }} id="rightTop" className="btrigger">
  35. 右上
  36. </Button>
  37. );
  38. const rightBottom = (
  39. <Button style={{ margin: "5px" }} id="rightBottom" className="btrigger">
  40. 右下
  41. </Button>
  42. );
  43. const bottomLeft = (
  44. <Button style={{ margin: "5px" }} id="bottomLeft" className="btrigger">
  45. 下左
  46. </Button>
  47. );
  48. const bottomRight = (
  49. <Button style={{ margin: "5px" }} id="bottomRight" className="btrigger">
  50. 下右
  51. </Button>
  52. );
  53. const leftTop = (
  54. <Button style={{ margin: "5px" }} id="leftTop" className="btrigger">
  55. 左上
  56. </Button>
  57. );
  58. const leftBottom = (
  59. <Button style={{ margin: "5px" }} id="leftBottom" className="btrigger">
  60. 左下
  61. </Button>
  62. );
  63. const App = () => (
  64. <div style={{ paddingLeft: 220, paddingTop: 100 }}>
  65. <div style={{ marginLeft: 75 }}>
  66. <Tooltip trigger={topLeft} align="tl" text="text text" />
  67. <Tooltip trigger={top} align="t" text="text text" />
  68. <Tooltip trigger={topRight} align="tr" text="text text" />
  69. </div>
  70. <div style={{ width: 80, float: "left" }}>
  71. <Tooltip trigger={leftTop} align="lt" text="text text" />
  72. <Tooltip trigger={left} align="l" text="text text" />
  73. <Tooltip trigger={leftBottom} align="lb" text="text text" />
  74. </div>
  75. <div style={{ width: 80, marginLeft: 290 }}>
  76. <Tooltip trigger={rightTop} align="rt" text="text text" />
  77. <Tooltip trigger={right} align="r" text="text text" />
  78. <Tooltip trigger={rightBottom} align="rb" text="text text" />
  79. </div>
  80. <div style={{ marginLeft: 80, clear: "both" }}>
  81. <Tooltip trigger={bottomLeft} align="bl" text="text text" />
  82. <Tooltip trigger={bottom} align="b" text="text text" />
  83. <Tooltip trigger={bottomRight} align="br" text="text text" />
  84. </div>
  85. </div>
  86. );
  87. ReactDOM.render(<App />, mountNode);
  1. .code-box-demo .sui-btn {
  2. margin-right: 1em;
  3. margin-bottom: 1em;
  4. }

三种触发方式

鼠标移入、聚集、点击。

Balloon 气泡提示 - 图8

查看源码在线预览

  1. import { Button, Balloon } from "@icedesign/base";
  2. const content = (
  3. <div>
  4. <p>内容</p>
  5. </div>
  6. );
  7. const MoveTarget = <Button style={{ margin: "5px" }}>移入</Button>;
  8. const ClickTarget = <Button style={{ margin: "5px" }}>点击</Button>;
  9. const FocusTarget = <Button style={{ margin: "5px" }}>获取焦点</Button>;
  10. const App = () => (
  11. <div>
  12. <Balloon trigger={MoveTarget} triggerType="hover">
  13. {content}
  14. </Balloon>
  15. <Balloon trigger={ClickTarget} triggerType="click">
  16. {content}
  17. </Balloon>
  18. <Balloon trigger={FocusTarget} triggerType="focus">
  19. {content}
  20. </Balloon>
  21. </div>
  22. );
  23. ReactDOM.render(<App />, mountNode);

相关区块

Balloon 气泡提示 - 图9

暂无相关区块