Style Vue slots one by one

I am a beginner delving into the world of vue (specifically, vue 2) and aiming to grasp the concept of utilizing slots in the most effective manner. Below is the code I'm working with:

   <template>
        <div>
            <button class="btn {{ In this section, I aim to be able to switch between btn-one and btn-three}}">
                <slot></slot>
            </button>
        </div>
    </template>
    
   <style scoped>
    .btn {
        margin: 20px 0px;
        height: 50px;
        text-align: center;
        width: 250px;
        cursor: pointer;
    }

    /* 
    ========================
        BUTTON ONE
    ========================
    */

    .btn-one {
        background-color: #FF6766;
        color: #FFF;
        transition: all 0.3s;
        position: relative;
    }

    .btn-one span {
        transition: all 0.3s;
    }

    .btn-one::before {
        content: '';
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 1;
        opacity: 0;
        transition: all 0.3s;
        border-top-width: 1px;
        border-bottom-width: 1px;
        border-top-style: solid;
        border-bottom-style: solid;
        border-top-color: rgba(255,255,255,0.5);
        border-bottom-color: rgba(255,255,255,0.5);
        transform: scale(0.1, 1);
    }

    .btn-one:hover span {
        letter-spacing: 2px;
    }

    .btn-one:hover::before {
        opacity: 1; 
        transform: scale(1, 1); 
    }

    .btn-one::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 1;
        transition: all 0.3s;
        background-color: rgba(255,255,255,0.1);
    }
    
    .btn-one:hover::after {
        opacity: 0; 
        transform: scale(0.1, 1);
    }


        /* 
    ========================
        BUTTON THREE
    ========================
    */
    .btn-three {
        color: #FFF;
        transition: all 0.5s;
        position: relative;
        background-color: #66A182;;
    }

    .btn-three::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 1;
        background-color: rgba(255,255,255,0.1);
        transition: all 0.3s;
    }

    .btn-three:hover::before {
        opacity: 0 ;
        transform: scale(0.5,0.5);
    }

    .btn-three::after {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 1;
        opacity: 0;
        transition: all 0.3s;
        border: 1px solid rgba(255,255,255,0.5);
        transform: scale(1.2,1.2);
    }

    .btn-three:hover::after {
        opacity: 1;
        transform: scale(1,1);
    }

</style>

My objective now is to implement it as follows:

   <template>
    <div class="page-margins">
        <h1>Testing out the slot functionality</h1>

        <BaseButton>
            <div class="position">
                <span class="button-span">DOWNLOAD</span>
                <span class="material-icons">file_download</span>        
            </div>
        </BaseButton>

        <BaseButton>
            <div class="position">
                <span class="button-span">CANCEL</span>
                <span class="material-icons">close</span>        
            </div>
        </BaseButton> 
    </div>
</template>

<script>

import BaseButton from '../components/BaseButton.vue'

    export default {

        components: {
            BaseButton
        }
        
    }
</script>

<style scoped>
    .page-margins{
        margin: 100px 50px;;
    }

    .material-icons{
        font-size: 25px;
        letter-spacing: normal;
        display: inline-block;
        white-space: nowrap;
    }

    .position {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: center;
    }

</style>

Therefore, my question is: How can I modify from using btn-one to btn-three since currently both buttons appear red when I wish to use btn-three for the second button...

I appreciate your assistance.

Answer №1

To apply CSS attributes directly to the button component, remove the <div> inside of it.

If your BaseButton is set up like this:

<template>
    <button class="btn">
        <slot></slot>
    </button>
</template>

You can then use it as follows:

<BaseButton class="btn-three">
    <span class="position">
         <span class="button-span">DOWNLOAD</span>
         <span class="material-icons">file_download</span>        
    </span>
</BaseButton>

This way, the button will have the class attribute of btn btn-three. Different buttons may have different classes based on your implementation.

Additionally, avoid using <div> within a <button> since only phrasing content is permitted here.

Answer №2

Give this a try

{{#if something}}
    <button class="btn-one">
        <slot></slot>
    </button>
{{/if}}

{{#if somethingThree}}
     <button class="btn-three">
          <slot></slot>
     </button>
{{/if}}

You can also opt for the following:

<button v-if="something" class="btn-one">
     <slot></slot>
</button>

 <button v-if="somethingThree" class="btn-three">
     <slot></slot>
</button>

To find out more:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Optimizing CSS to eliminate extra space from the first button in the navigation menu bar

I'm currently working on creating a simple CSS-based navigation bar, but I've encountered an issue. The first item in my HTML list seems to have extra space to its left that I can't seem to remove. Check out the live example here Also avai ...

Coldfusion: The Troubles of an Empty Link href="#" Error

For my CFWheels Coldfusion project, I have been utilizing Adobe Coldfusion Builder 3. In Coldfusion, when you want to output a variable, you typically do something like this: #variable_name# However, I am interested in incorporating an empty link into m ...

Learn the steps for accessing and utilizing aria-label text in Google Chrome

Struggling with the aria-label attribute for blind users in Chrome. Can't seem to hear the aria-label text. Any insights on how aria-label behaves in Chrome or if I'm using the wrong approach? If I'm heading in the wrong direction or using ...

Utilize Bootstrap and Flat UI to enhance the design of a div, or duplicate the styles to apply to the navbar

My navigation bar was created using Bootstrap and Flat UI on top of it. However, when I try to incorporate bootstrap+flat ui into another HTML+CSS file that I have, the hexagons end up looking distorted. It seems like my navigation bar only works with flat ...

The post operation fails to include data in the table

I've been trying to add a user to my table, but nothing seems to be happening. Can someone help me figure out what I'm missing? .wrapper.d-flex.flex-column br input#fname(type='text', name='text' placeholder="name ...

Make sure to keep Vue-Cookies intact even when closing the browser or tab

I have integrated vue-cookies into my Vue application. The code I'm using to store a cookie is as follows: $cookies.set('authUser', authUserObj); The object authUserObj contains the access_token. However, when I close and reopen the ta ...

Is it possible to utilize the lighten css property within ngStyle in Angular?

Having some trouble with the lighten property in ngStyle and it's not working as expected. I have a color variable within my component that I want to utilize like so: <div [ngStyle]="{color: schedule.group.color, background: 'lighten(' ...

The class "slick" in <col class="slick"> does not add any styling or effects

My interpretation of the col element is that it can be used to assign a class to all elements in a column of a table. However, I am experiencing difficulties with this approach. While I am able to apply the class to individual td elements, I am looking for ...

LovelyStew - various cards containing numerous nested attributes

In an attempt to view all "cards" on the California State Lotto page: There are X draws, each represented by a 'Card': Sample Card <div class="card"><div class="card-header" id="heading-1"><button class="accordion--toggle w-100 ...

What is the method for retrieving Vue Js computed properties?

I am facing an issue with my VueJS code. I have a method that calls axios post and sets the authenticated user correctly (the console displays the user). However, when I try to call the computed property in the component, the user turns out to be empty. e ...

Modify picture properties when listbox is altered using jQuery

I need to set up a unique album gallery display where different folders are selected based on the item chosen in a selectbox. Below is the HTML code: <select name="album" id="album" onChange="changeimage();"> <option value="0" selected disab ...

How can I design a tooltip window using HTML, jQuery, or other elements to create a box-like effect?

As someone who is new to front end development, I am facing a challenge with my web app. The requirement is that when a user hovers over an image, I need a 'box' to open nearby and display a pie chart. Although I have managed to get the hover fu ...

When I toggle the div to close on mobile, I want it to show on desktop

My attempt at creating a toggle button to hide and show content was not successful. I struggle with coding in Javascript/Jquery. In the desktop view, the description displays perfectly (see image here), but in mobile view, there is a button to toggle hidi ...

Use a Vue filter to refine search results by limiting the items displayed rather than expanding them

In my example, I have elements that need to be filtered. If the filter selection is precise, there should be very few or no items displayed because the conditions are rarely met. However, in the current source code, the filter is adding elements instead of ...

Utilize a specific component to enclose particular words within a contenteditable div in an Angular project

I have a task at hand where I need to manipulate text input from a contenteditable division, use regex to identify specific words, and then wrap those words within an angular component. Although I have managed to successfully replace the text with the com ...

CSS slideshow animation is not functioning properly

Hey there, I'm new around here and I've got a question for you all. Please bear with me if I make any mistakes. So, I'm attempting to create a simple CSS slide image. In my animation code, I want the picture to fade from the left every time ...

How can I link an image source from a JSON file to my template?

Hello, I am new to VueJS and I am encountering an issue. I am trying to connect the IMG src in my template with a URL that is specified in my JSON file. For example, if I have products and I want to display the full logo for each item, I need to link the ...

Getting the ID of a select input option in Vue.js using FormulateInput

Recently, I've been working with a FormulateInput select component that looks like this: <FormulateInput name="broj_zns-a" @change="setZns" :value="brojZnsa" type="select" label="Broj ZNS- ...

Sending a post request to a Spring controller with ajax: Step-by-step guide

I am having trouble sending a post request to the spring controller using ajax. It seems that something is not working correctly. If anyone can help me figure out what I did wrong, I would greatly appreciate it. $("#myprofile").on('submit&ap ...

In order to properly set up Require JS, you will need to configure the JS settings

Is there a way to specify the path from any property inside the require JS config section? We are trying to pass a property inside the config like so: The issue at hand: var SomePathFromPropertyFile = "CDN lib path"; require.config({ waitSeconds: 500 ...