Reduced complications with mixin scopes

I have developed a parametric mixin that utilizes a unique "node-value" system to locate the children of an element. The process involves detecting the children through a loop function named ".extractArrays", which contains ".deliverChild" within it. Subsequently, the discovered children (maximum of 4) are fed into ".calculateWidth", which then calculates and stores the width of each child in specific variables (e.g., "calculatedWidth1").

The challenge arises when there are no third and fourth children present - this causes errors as @child3 and @child4 remain undefined. To address this issue, I initialized @child3 and @child4 with zero values prior to invoking the mixins, intending for them to default to 0 before being accessed by the mixins. Strangely, even when a third child is identified, the returned value of @child3 from the mixin fails to override the initial value of 0 set for @child3. This puzzling behavior has led me to question certain aspects of Less, particularly regarding mixins and their scope.

@child3: none, 0 0, 0, 0;
@child4: none, 0 0, 0, 0;

    .extractArrays(@index, @node, @node-value, @elements-array) when (@index <= @length-elements-array) {
        @element-array: extract(@elements-array, @index);
        @found-node: extract(@element-array, 2);
        .deliverChild(@element-array, @found-node) when (@found-node = @child-node-value1) {
            @child1: extract(@element-array, 1);
        }
        .deliverChild(@element-array, @found-node) when (@found-node = @child-node-value2) {
            @child2: extract(@element-array, 1);
        }
        .deliverChild(@element-array, @found-node) when (@found-node = @child-node-value3) {
            @child3: extract(@element-array, 1);
        }
        .deliverChild(@element-array, @found-node) when (@found-node = @child-node-value4) {
            @child4: extract(@element-array, 1);
        }
        .deliverChild(@element-array, @found-node);
        .extractArrays(@index + 1, @node, @node-value, @elements-array);
    }
    .extractArrays(1, @node, @node-value, @elements-array);


    .calculateWidth(@child1, 1);
    .calculateWidth(@child2, 2);
    .calculateWidth(@child3, 3);

    width: @calculatedWidth1 + @calculatedWidth2 + @calculatedWidth3 + @calculatedWidth4;

Answer №1

(Here is a different algorithm that can serve as a starting point for an alternative approach suggested in the above comments):

A generic array filtering mixin could take this form (for Less 1.7.5 or newer):

@array: // key value
    a 1,
    b 2,
    c 3,
    a 4,
    d 5,
    a 6,
    c 7;

// how to use:
usage {
   .filter-array(@array, a); 
    result: @filter-array;
}

// implementation:
.filter-array(@array, @key, @key-index: 1) {
    .-(length(@array));
    .-(@i, @r...) when (@i > 0)
        and not(@key = extract(extract(@array, @i), @key-index)) {
            // skip item if key doesn't match
            .-((@i - 1), @r);
    }
    .-(@i, @r...) when (length(@r) > 0)
        and (@key = extract(extract(@array, @i), @key-index)) {
            // concatenate current item to @r if key matches and @r is not empty
            .-((@i - 1); extract(@array, @i), @r);
    }
    .-(@i, @r...) when (length(@r) = 0)
        and (@key = extract(extract(@array, @i), @key-index)) {
            // first item if key matches and @r is empty
            .-((@i - 1), extract(@array, @i));
    }
    .-(@i, @r...) when (@i = 0) {
        // set it as "return" variable:
        @filter-array: @r;
    }
}

The resulting filtered array would be:

@filter-array:
    a 1,
    a 4,
    a 6;

Although complex, this method is cleaner and more manageable compared to the original approach mentioned.

It might also be helpful to include a specific mixin to calculate the sum (or any other transformation resulting in a single value) of certain array values. This can simplify the implementation by eliminating the need for concatenation and some conditions in the above algorithm.

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

The background image is failing to display

I am currently working on a subpage for my website that includes a navigation bar and footer. I am looking to add a background image behind the navigation bar, similar to this design. https://i.stack.imgur.com/j6fDZ.jpg After trying various methods, I at ...

Using PHPs nth-child in media queries

There are multiple images on MyPage, typically displayed in rows of 6. However, the number of images per row adjusts based on browser size using media queries. The issue arises with the margin set for the last image in each row, as the nth-child property r ...

Tips for adding an offset to a #link located on a different webpage

I have a website that consists mainly of an index.html file with a few additional subpages. The index.html file is divided into sections, but the navigation has a fixed position and a height of 100px, which requires a 100px offset for links like <a hre ...

Swap out a term in a sentence with an interactive span element in real-time

I'm struggling with a seemingly simple task, and I feel quite embarrassed that I can't seem to solve it. My goal is to identify words in a string that begin with '@' or '#' and change their color to blue. The string itself com ...

Is there a way to have a span update its color upon clicking a link?

I have 4 Spans and I'm looking to create an interactive feature where clicking a link in a span changes the color of that span. Additionally, when another link in a different span is clicked, the color of that span changes while reverting the previous ...

What is the best way to center text vertically within an image?

How can I vertically align a blog entry title on an entry thumbnail image? The image size changes with the window size and the title varies in length for each entry. After finding a website that successfully achieved this effect, I tried replicating it us ...

Div element obstructing the ability to view markers

Issue with a positioned DIV over Google Maps: The DIV is causing accessibility problems for the map, taking up the entire width when it should stop at the green square borders. Interaction with the map is limited to the very bottom after the green square. ...

Database-driven menu selection

I am currently working on creating a dynamic side navigation menu that pulls its content from a database. The goal is to have the main categories displayed on the left-hand side, and when one is clicked, the others will slide down to make room for any subc ...

Replace existing styled-component CSS properties with custom ones

One of my components includes a CheckBox and Label element. I want to adjust the spacing around the label when it's used inside the CheckBox. Can this be achieved with styled()? Debugging Info The issue seems to be that the className prop is not b ...

placing a command button to the right side

Currently, I have successfully implemented a graphical solution using jsf2.2 primefaces 6.0 to allow users to view, download, and print pictures through the galleria component of primefaces. However, I am facing an issue with positioning the print command ...

Accordion styling using CSS

My current issue lies with setting the style for the contents of an accordion. The problem arises when the styles defined in a CSS file do not take effect on the content of my accordion when using the following code along with a separate CSS file: <scr ...

Tips for extracting links from a webpage using CSS selectors and Selenium

Is there a way to extract the HTML links per block on a page that renders in JavaScript? Would using CSS or Selenium be more effective than BeautifulSoup? If so, how would I go about utilizing either of those methods to achieve the extraction of the HTML ...

Implementing dynamic styles for a checked mat-button-toggle in Angular 6

How can I customize my button-toggle when it is checked? The current code I have doesn't seem to be working... This is the code snippet: <mat-button-toggle-group #mytoggle (change)="selectoption($event)" value="{{num}}"> <mat-bu ...

How can I use D3.js to form a circular group in an organization structure, link it with a circular image, and connect

Is it possible to create a radial grouped circle using d3.js, similar to the image below: I have written some code as shown below. However, I am facing challenges in connecting every circle with a curved line and displaying a tooltip when hovering over a ...

Creating a scrolling background effect using HTML and CSS: ``Background Parallax

I'm trying to keep the background of my HTML document fixed in the upper left corner, so that when a user scrolls through the page, they only see parts of it. Here's the code I'm using: <body style="background-image: url(background.png) ...

I'm having trouble adding a background image, even though I have set it to static. What could be

I attempted to add a background image to my Django website, but unfortunately, it was not successful. I followed the steps provided in this Stack Overflow answer here, however, it did not work. I even made changes to the database by migrating them, but s ...

Can you show me the way to open a single card?

Can someone assist me in making it so only one card opens when clicked, rather than all of them opening at once? Additionally, if there is already an open card and I click on another one, the currently open card should close automatically. If you have any ...

What strategies can we implement to ensure consistency in visual styles and templates across our microservices?

Our team is currently working on multiple microservices simultaneously. While traditional microservice architecture discourages code sharing and reuse between services, we are considering the benefits of consistent styling across all services that have Web ...

Changing Background Color on Div Click

After spending a considerable amount of time on this, I find myself getting confused and stuck. It seems like I might be overlooking something crucial. Essentially, my code is designed to have the default div background (gamebg), and upon clicking one of t ...

Setting the opacity of an image will also make the absolute div transparent

I currently have an image gallery set up with the following structure : <div class="gallery"> <div class="message"> Welcome to the gallery </div> <img src="http://placehold.it/300x300" /> </div> Here ...