|
| 1 | +<template> |
| 2 | + <div class="mt-5"> |
| 3 | + <div class="flex flex-wrap"> |
| 4 | + <div class="color-box" |
| 5 | + :class="{ selected: hasColor(color.hex) }" |
| 6 | + v-for="(color, index) in colors" |
| 7 | + :key="index" |
| 8 | + :style="{ 'background-color': color.hex }" |
| 9 | + @contextmenu.prevent="$refs.menu.open($event, color)" |
| 10 | + > |
| 11 | + |
| 12 | + </div> |
| 13 | + </div> |
| 14 | + |
| 15 | + <h5 class="my-4"> |
| 16 | + Selected colors: |
| 17 | + </h5> |
| 18 | + <code class="mb-3">{{ selectedColors }}</code> |
| 19 | + |
| 20 | + <vue-context ref="menu"> |
| 21 | + <template slot-scope="child" v-if="child.data"> |
| 22 | + <li> |
| 23 | + <a href="#" @click.prevent="toggle(child.data)"> |
| 24 | + {{ hasColor(child.data.hex) ? 'Remove Color' : 'Select Color' }} |
| 25 | + </a> |
| 26 | + </li> |
| 27 | + </template> |
| 28 | + </vue-context> |
| 29 | + </div> |
| 30 | +</template> |
| 31 | + |
| 32 | +<script> |
| 33 | + import VueContext from 'vue-context'; |
| 34 | + import 'vue-context/src/sass/vue-context.scss'; |
| 35 | +
|
| 36 | + export default { |
| 37 | + components: { VueContext }, |
| 38 | +
|
| 39 | + data () { |
| 40 | + return { |
| 41 | + colors: [ |
| 42 | + { name: 'red', hex: '#f44336' }, |
| 43 | + { name: 'blue', hex: '#2196F3' }, |
| 44 | + { name: 'cyan', hex: '#00BCD4' }, |
| 45 | + { name: 'green', hex: '#4CAF50' }, |
| 46 | + { name: 'orange', hex: '#FF9800' } |
| 47 | + ], |
| 48 | + selectedColors: [] |
| 49 | + }; |
| 50 | + }, |
| 51 | +
|
| 52 | + methods: { |
| 53 | + colorIndex (hex) { |
| 54 | + return this.selectedColors.findIndex(color => color.hex === hex); |
| 55 | + }, |
| 56 | +
|
| 57 | + hasColor (hex) { |
| 58 | + return this.colorIndex(hex) > -1; |
| 59 | + }, |
| 60 | +
|
| 61 | + toggle (color) { |
| 62 | + const index = this.colorIndex(color.hex); |
| 63 | +
|
| 64 | + if (index > -1) { |
| 65 | + this.$delete(this.selectedColors, index); |
| 66 | + } else { |
| 67 | + this.selectedColors.push(color); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + }; |
| 72 | +</script> |
| 73 | + |
| 74 | +<style lang="scss" scoped> |
| 75 | + .color-box { |
| 76 | + width: 100px; |
| 77 | + height: 100px; |
| 78 | + margin-right: 10px; |
| 79 | + border-radius: 4px; |
| 80 | + box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4); |
| 81 | + opacity: .6; |
| 82 | +
|
| 83 | + &.selected { |
| 84 | + opacity: 1; |
| 85 | + } |
| 86 | +
|
| 87 | + &:last-child { |
| 88 | + margin-right: 0; |
| 89 | + } |
| 90 | + } |
| 91 | +</style> |
0 commit comments