Goldy
Goldy

Reputation: 41

How to show box shadow only when certain tab is selected?

For this website, I have two login tabs as seen in the image below and for the selected tab it shows an icon next to it, but to make it more visible that this tab is selected I want to add some sorta box-shadow or something, but for now, it shows the box-shadow for both of the tabs and I just can't get my head around how to make it only show for the selected Tab.

enter image description here

This is the code that I have - I tried to add v-if statement, but it won't work as I already have v-for so If there is any way how to make this work I would be really happy if you could help me out here. Thank you in advance!

<template>
  <v-main id="login">
    <v-container fill-height fluid>
      <v-layout align-center justify-center>
        <v-flex md4 sm8 xs12>
          <v-card class="elevation-12">
            <v-toolbar color="primary" dark>
              <v-toolbar-title>
                <v-icon left> mdi-login-variant </v-icon>
                {{ $t("welcome") }}
              </v-toolbar-title>
            </v-toolbar>
            <v-divider />
            <v-tabs v-model="selectedTab" grow hide-slider>
              <v-tab
                v-for="(tab, i) in tabs"
                :key="i"
                :class="{
                  'primary white--text': tab == selectedTab,
                  caption: tab != selectedTab,
                }"
                :href="`#${tab}`"
                id="boxShadow" //This is where I added the ID 
                class="pa-0"
              >
                {{ tab }}
                <v-icon id="IconPosition" v-if="tab === selectedTab"
                  >mdi-login-variant</v-icon
                >
              </v-tab>
              <v-tab-item
                v-for="(tab, i) in tabs"
                :key="i"
                :value="tab"
                reverse-transition="scale-transition"
                transition="scale-transition"
              >
                <v-divider />
                <v-card-text>
                  <v-form @submit.prevent="login">
                    <v-text-field
                      v-model.lazy="username"
                      :label="$t('username')"
                      :prepend-inner-icon="
                        tab === 'Windows'
                          ? 'mdi-microsoft-windows'
                          : 'mdi-account'
                      "
                      :rules="[username !== null || required]"
                      name="username"
                      outlined
                      placeholder=" "
                      type="text"
                    />
                    <v-text-field
                      v-model.lazy="password"
                      :label="$t('password')"
                      :rules="[password !== null || required]"
                      name="password"
                      outlined
                      placeholder=" "
                      prepend-inner-icon="mdi-lock"
                      type="password"
                    />
                    <!-- If error, rended error component -->
                    <error-view
                      v-if="error"
                      :error="error"
                      :is-login="true"
                      class="pa-0"
                    />
                    <v-card-actions class="pa-0">
                      <v-spacer />
                      <v-btn :loading="loading" color="primary" type="submit">
                        {{ $t("submit") }}
                      </v-btn>
                    </v-card-actions>
                  </v-form>
                </v-card-text>
              </v-tab-item>
            </v-tabs>
            <div id="version-div">
              <app-version />
            </div>
          </v-card>
        </v-flex>
      </v-layout>
    </v-container>
  </v-main>
</template>

<script>
import AppVersion from "@/components/version";

const errorView = () => import("@/components/errorView");

export default {
  name: "Login",
  components: {
    errorView,
    AppVersion,
  },

  data() {
    return {
      tabs: ["Windows", "Standard"],
      selectedTab: "Standard",
      username: null,
      password: null,
      loading: false,
      error: null,
      required: (value) => !!value || this.$t("req"),
    };
  },

  methods: {
    resetForm(value) {
      this.username = this.password = value;
    },
    login() {
      if (!this.username || !this.password) {
        this.error = this.$t("warn");
        this.resetForm(null);
      } else {
        this.loading = true;
        const encodedPass = window.btoa(
          unescape(encodeURIComponent(this.password))
        );
        this.$store
          .dispatch("retrieveUser", {
            username: this.username,
            password: encodedPass,
            outside: this.selectedTab === "Windows" ? false : true,
          })
          .then(() => {
            this.$router.push({ name: "home" });
            this.error = null;
          })
          .catch((error) => {
            this.error = error;
          })
          .finally(() => {
            this.resetForm("");
            this.loading = false;
          });
      }
    },
  },
};
</script>

<style>
#boxShadow {
  -moz-box-shadow: inset 0 0 10px #000000;
  -webkit-box-shadow: inset 0 0 10px #000000;
  box-shadow: inset 0 0 10px #000000;
}
</style>

Upvotes: 0

Views: 350

Answers (1)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

the id attribute should be unique, in your case the rendered tab will have the same id, try out to name the #boxShadow to a class .boxShadow like :

<style>
.boxShadow {
  -moz-box-shadow: inset 0 0 10px #000000;
  -webkit-box-shadow: inset 0 0 10px #000000;
  box-shadow: inset 0 0 10px #000000;
}
</style>

then bind it conditionally :

          <v-tab
            v-for="(tab, i) in tabs"
            :key="i"
            :class="{
              'primary white--text': tab == selectedTab,
              caption: tab != selectedTab,
               boxShadow: tab === selectedTab,
            }

or just :

            :class="{
              'primary white--text boxShadow': tab == selectedTab,
              caption: tab != selectedTab,
            }

Upvotes: 1

Related Questions