fs-popover.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <template>
  2. <view class="fs-popover" :style="{ 'z-index': zIndex }">
  3. <view class="fs-popover-refer" @click="handleClickRefer"><slot name="refer"></slot></view>
  4. <view
  5. class="fs-popover-action"
  6. v-if="actionVisible"
  7. :class="['fs-popover-' + placement]"
  8. :style="{ 'z-index': zIndex }"
  9. @click="handleClose"
  10. >
  11. <view class="fs-popover-arrow"></view>
  12. <slot>
  13. <view
  14. class="fs-popover-action-item line1"
  15. :class="{ 'fs-popover-active': item[valueKey] && item[valueKey] === modelValue[valueKey] }"
  16. v-for="(item, index) in actions"
  17. :key="index"
  18. @click="handleClickAction(item)"
  19. >
  20. {{ item.text }}
  21. </view>
  22. </slot>
  23. </view>
  24. <fs-mask v-model="actionVisible" :z-index="1" bgColor="rgba(0,0,0,0)"></fs-mask>
  25. </view>
  26. </template>
  27. <script>
  28. /**
  29. * 气泡弹出层组件
  30. * @description 气泡弹出层组件
  31. * @property {String} valueKey 作为 value 唯一标识的键名
  32. * @property {Array} actions 选项列表
  33. * @property {String} placement = [top | top-start | top-end | bottom | bottom-start | bottom-end,left | left-start | left-end,right | right-start | right-end] 弹出位置
  34. * @property {String} bgColor 背景色
  35. * @property {String} textColor 文字颜色
  36. * @property {String} activeColor 文字选中颜色
  37. * @property {String} borderColor 边框颜色
  38. * @property {String} width 气泡弹出层宽度
  39. * @property {String} actionWidth 选项列表宽度
  40. */
  41. export default {
  42. name: 'fs-popover'
  43. }
  44. </script>
  45. <script setup>
  46. import { ref, onMounted, watch } from 'vue'
  47. import utils from '@/utils/utils'
  48. const props = defineProps({
  49. modelValue: [Object],
  50. valueKey: {
  51. type: String,
  52. default: 'id'
  53. },
  54. placement: {
  55. type: String,
  56. default: 'bottom',
  57. validator(value) {
  58. return [
  59. 'top',
  60. 'top-start',
  61. 'top-end',
  62. 'bottom',
  63. 'bottom-start',
  64. 'bottom-end',
  65. 'left',
  66. 'left-start',
  67. 'left-end',
  68. 'right',
  69. 'right-start',
  70. 'right-end'
  71. ].includes(value)
  72. }
  73. },
  74. actions: Array,
  75. bgColor: {
  76. type: String,
  77. default: '#fff'
  78. },
  79. textColor: {
  80. type: String,
  81. default: '#666'
  82. },
  83. activeColor: {
  84. type: String,
  85. default: '#165DFF'
  86. },
  87. borderColor: {
  88. type: String,
  89. default: '#E8EAF2'
  90. },
  91. width: {
  92. type: String,
  93. default: 'auto'
  94. },
  95. actionWidth: {
  96. type: String,
  97. default: 'auto'
  98. }
  99. })
  100. const emits = defineEmits(['clickAction', 'visibleChange', 'update:modelValue'])
  101. const uuid = utils.uuid()
  102. const actionVisible = ref(false)
  103. const handleClickRefer = () => {
  104. uni.$emit('popoverClose', { uuid: uuid })
  105. actionVisible.value = !actionVisible.value
  106. }
  107. watch(actionVisible, () => {
  108. emits('visibleChange')
  109. })
  110. const handleClickAction = item => {
  111. emits('update:modelValue', item)
  112. emits('clickAction', item)
  113. }
  114. const handleClose = () => {
  115. actionVisible.value = false
  116. }
  117. const zIndex = ref(10)
  118. uni.$on('popoverClose', res => {
  119. if (uuid !== res.uuid) {
  120. zIndex.value = 10
  121. handleClose()
  122. } else {
  123. zIndex.value = 20
  124. }
  125. })
  126. defineExpose({
  127. handleClose
  128. })
  129. </script>
  130. <style lang="scss" scoped>
  131. $margin: 20rpx;
  132. $arrowOffset: 30rpx;
  133. $arrowSize: 24rpx;
  134. .fs-popover {
  135. position: relative;
  136. display: inline-block;
  137. width: v-bind(width);
  138. &-action {
  139. position: absolute;
  140. min-width: 260rpx;
  141. max-width: 100%;
  142. width: v-bind(actionWidth);
  143. background-color: v-bind(bgColor);
  144. transition: opacity 0.15s, transform 0.15s;
  145. border-radius: 12rpx;
  146. padding: 0 30rpx;
  147. color: v-bind(textColor);
  148. border: 2rpx solid v-bind(borderColor);
  149. &-item {
  150. padding: 20rpx;
  151. & + & {
  152. border-top: 2rpx solid v-bind(borderColor);
  153. }
  154. }
  155. }
  156. &-active {
  157. color: v-bind(activeColor);
  158. }
  159. &-arrow {
  160. position: absolute;
  161. width: $arrowSize;
  162. height: $arrowSize;
  163. transform: rotate(45deg);
  164. z-index: 1000;
  165. background-color: v-bind(bgColor);
  166. border: 2rpx solid transparent;
  167. margin-top: -$arrowSize / 2;
  168. margin-left: -$arrowSize / 2;
  169. }
  170. &-top {
  171. left: 50%;
  172. bottom: 100%;
  173. transform: translateX(-50%);
  174. margin-bottom: $margin;
  175. .fs-popover-arrow {
  176. border-bottom-color: v-bind(borderColor);
  177. border-right-color: v-bind(borderColor);
  178. bottom: -$arrowSize / 2;
  179. left: 50%;
  180. }
  181. }
  182. &-top-start {
  183. left: 0;
  184. bottom: 100%;
  185. margin-bottom: $margin;
  186. .fs-popover-arrow {
  187. border-bottom-color: v-bind(borderColor);
  188. border-right-color: v-bind(borderColor);
  189. bottom: -$arrowSize / 2;
  190. left: $arrowOffset;
  191. margin-left: 0;
  192. }
  193. }
  194. &-top-end {
  195. right: 0;
  196. bottom: 100%;
  197. margin-bottom: $margin;
  198. .fs-popover-arrow {
  199. border-bottom-color: v-bind(borderColor);
  200. border-right-color: v-bind(borderColor);
  201. bottom: -$arrowSize / 2;
  202. right: $arrowOffset;
  203. margin-left: 0;
  204. }
  205. }
  206. &-bottom {
  207. left: 50%;
  208. top: 100%;
  209. transform: translateX(-50%);
  210. margin-top: $margin;
  211. .fs-popover-arrow {
  212. border-top-color: v-bind(borderColor);
  213. border-left-color: v-bind(borderColor);
  214. top: 0;
  215. left: 50%;
  216. }
  217. }
  218. &-bottom-start {
  219. left: 0;
  220. top: 100%;
  221. margin-top: $margin;
  222. .fs-popover-arrow {
  223. border-top-color: v-bind(borderColor);
  224. border-left-color: v-bind(borderColor);
  225. top: 0;
  226. left: $arrowOffset;
  227. margin-left: 0;
  228. }
  229. }
  230. &-bottom-end {
  231. right: 0;
  232. top: 100%;
  233. margin-top: $margin;
  234. .fs-popover-arrow {
  235. border-top-color: v-bind(borderColor);
  236. border-left-color: v-bind(borderColor);
  237. top: 0;
  238. right: $arrowOffset;
  239. margin-left: 0;
  240. }
  241. }
  242. &-left {
  243. left: -$margin;
  244. top: 50%;
  245. transform: translate(-100%, -50%);
  246. .fs-popover-arrow {
  247. border-bottom-color: v-bind(borderColor);
  248. border-right-color: v-bind(borderColor);
  249. top: 50%;
  250. right: -$arrowSize / 2;
  251. transform: rotate(-45deg);
  252. }
  253. }
  254. &-left-start {
  255. left: -$margin;
  256. top: 0;
  257. transform: translateX(-100%);
  258. .fs-popover-arrow {
  259. border-bottom-color: v-bind(borderColor);
  260. border-right-color: v-bind(borderColor);
  261. top: $arrowOffset;
  262. right: -$arrowSize / 2;
  263. transform: rotate(-45deg);
  264. margin-top: 0;
  265. }
  266. }
  267. &-left-end {
  268. left: -$margin;
  269. bottom: 0;
  270. transform: translateX(-100%);
  271. .fs-popover-arrow {
  272. border-bottom-color: v-bind(borderColor);
  273. border-right-color: v-bind(borderColor);
  274. bottom: $arrowOffset;
  275. right: -$arrowSize / 2;
  276. transform: rotate(-45deg);
  277. margin-top: 0;
  278. }
  279. }
  280. &-right {
  281. left: 100%;
  282. top: 50%;
  283. transform: translateY(-50%);
  284. margin-left: $margin;
  285. .fs-popover-arrow {
  286. border-bottom-color: v-bind(borderColor);
  287. border-left-color: v-bind(borderColor);
  288. top: 50%;
  289. left: 0;
  290. }
  291. }
  292. &-right-start {
  293. left: 100%;
  294. top: 0;
  295. margin-left: $margin;
  296. .fs-popover-arrow {
  297. border-bottom-color: v-bind(borderColor);
  298. border-left-color: v-bind(borderColor);
  299. top: $arrowOffset;
  300. left: 0;
  301. margin-top: 0;
  302. }
  303. }
  304. &-right-end {
  305. left: 100%;
  306. bottom: 0;
  307. margin-left: $margin;
  308. .fs-popover-arrow {
  309. border-bottom-color: v-bind(borderColor);
  310. border-left-color: v-bind(borderColor);
  311. left: 0;
  312. bottom: $arrowOffset;
  313. margin-top: 0;
  314. }
  315. }
  316. }
  317. </style>