fs-popover.vue 6.1 KB

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