so I am trying to achieve the following design:
Specifically the colourful badges on top. Now these items are grouped and can be any number. In the picture they are grouped into 2 but they can easily be 4 or 5.
I wanted a way to programmatically change the background and text colour of each badge for each group.
I have tried soo many things that haven't worked for me, at best I am currently only able to get the first colour to change.
this is my page:
<template>
<div class="w-full flex flex-col px-6">
<div class="flex flex-row">
<button class="flex flex-wrap justify-center content-center w-20 h-20 bg-blob-1 bg-no-repeat bg-contain bg-center -ml-3">
<img class="flex w-5 h-5" src="~/assets/images/icon-back.svg" alt="">
</button>
</div>
<div class="flex flex-col mt-1">
<span class="font-raleway font-black text-2xl text-white">{{ route.origin }} - {{ route.destination }}</span>
<div class="inline-block w-44 bg-black bg-opacity-50 rounded p-2">
<div class="font-raleway text-md text-white">{{ departureDate.formattedDate }}</div>
</div>
</div>
<div class="flex flex-col mt-10">
<type :name="name" :bg-color="colours[index].bg" :text-color="colours[index].text" v-for="(values, name, index) in schedules" :key="name" class="mb-10">
<schedule :schedule="schedule" v-for="schedule in values" :key="schedule.id" class="mb-1" />
</type>
</div>
</div>
</template>
<script>
import { mapState } from 'vuex';
import type from '~/components/bus/type.vue';
import schedule from '~/components/bus/schedule.vue';
export default {
name: 'schedules',
layout: 'bus-default',
components: {type, schedule},
async fetch({ store }) {
const trip = store.state.trip;
const schedule = store.state.schedule;
await store.dispatch('schedule/getSchedules', {
company: trip.company.alias,
origin: trip.route.origin,
destination: trip.route.destination,
date: trip.departureDate.fullDate,
schedules: schedule.schedules
});
},
computed: {
...mapState({
company: state => state.trip.company.name,
route: state => state.trip.route,
departureDate: state => state.trip.departureDate,
schedules: state => state.schedule.schedules,
colours: state => state.schedule.colours
}),
}
}
</script>
This is my component that contains the badge:
<template>
<div>
<div :class="{bgColor: true, textColor}" :key="bgColor" class="w-auto inline-block rounded-full px-3 py-1 ml-3 absolute z-20 shadow-md -mt-4 font-raleway text-sm capitalize">{{ name }}</div>
<slot></slot>
</div>
</template>
<script>
import { mapState } from 'vuex';
export default {
name: 'type',
props: ['name', 'bg-color', 'text-color'],
computed: {
...mapState({
colours: state => state.schedule.colours
}),
}
}
</script>
This is my store file:
export const state = () => ({
schedules: [],
schedule: {},
colours: [{'bg': 'bg-red-400', 'text': 'text-white'}, {'bg': 'bg-blue-400', 'text': 'text-white'}, {'bg': 'bg-yellow-600', 'text': 'text-gray-800'}],
colour: {}
});
export const mutations = {
setColours(state, colours) {
state.colours = colours;
},
setColour(state, colour) {
state.colour = colour;
},
setSchedules(state, schedules) {
state.schedules = schedules;
},
}
export const actions = {
async getSchedules({ commit }, params) {
const res = await this.$api.get('/bus/schedules', { params: params });
commit('setSchedules', res.data);
},
initialiseColours({ commit }) {
const colours = [{'bg': 'bg-red-400', 'text': 'text-white'}, {'bg': 'bg-blue-400', 'text': 'text-white'}, {'bg': 'bg-yellow-600', 'text': 'text-gray-800'}];
commit('setColours', colours);
},
getRandomColour({ commit, state }) {
var colours = [...state.colours];
console.log('colours:', colours);
var index = Math.floor(Math.random() * colours.length);
const colour = colours.splice(index, 1)[0];
commit('setColours', colours);
commit('setColour', colour);
},
}
So what I want to achieve here is to programmatically assign random background colours to each "badge" in each group. The badges I'm referring to are the executive and standard in the picture.
Also the text should be visible depending on the background, white when necessary or black when necessary.
For some reason my solution only changes the first item, the 2nd item is transparent however when I inspect HTML I see the class there but it doesn't show the colour in the browser.
Edit: So one last thing I forgot to add is colours should be used without replacement, meaning when one could has been used it should not repeat again.