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

How can Jquery detect when users click on 'ok' in confirm() pop-up dialogs?

Within my application, I have multiple confirmation dialogues that appear when users attempt to delete certain items. Is there a method to determine if a user has clicked the 'ok' button on any of these dialogues globally so that I can execute a ...

How can I make a div move to the position of another div and stay with it when the screen is resized?

In my card game project, I am dealing with an issue where cards are not aligning properly with the designated "dropZonePositions" when the screen is resized. Despite creating animations for the card movement that I'm satisfied with, the problem arises ...

Can the max-height CSS media-query be utilized safely?

It seems that using the max-height media query is proving to be quite a challenge due to an issue with Chrome 67 on iOS. The problem arises when users scroll on Chrome as it constantly adds and removes the address bar. This action causes the max-height va ...

Encircling the most recently selected image with a border

I have successfully made some <img> elements act like <input type="radio"> buttons and now I want to visually indicate the user's selection by adding a border around the chosen image. My challenge is that there are multiple rows of images ...

Adding a class to a clicked button in Vue.js

A unique function of the code below is showcasing various products by brand. When a user clicks on a brand's button, it will display the corresponding products. This feature works seamlessly; however, I have implemented a filter on the brands' lo ...

Placing an Image in the Right Corner Using jQuery Mobile

I am currently developing a mobile app and I need to align three images one below the other at the right corner next to some text. Any suggestions would be greatly appreciated. Thank you in advance. Below is my code snippet: <div> <ul data-r ...

What is the method, by using JavaScript or CSS, to include extra space at the end of text block without starting a new line?

Imagine having some text, and at the conclusion of each paragraph there is a (more)... link. The layout ends up looking like this: This is just an example paragraph. Click on the more link for additional information. (more...) Now comes the desire to i ...

Having difficulty customizing the background color of a navbar using CSS in Bootstrap 5

I've been attempting to customize the color of the navbar using a custom CSS code, but unfortunately without success. I've searched through various resources and tried different class names like navbar, navbar-custom, and more, but none seem to w ...

Aligning two images vertically using the display: table-cell property

I'm trying to align two images vertically using CSS' display: table-cell property, but it doesn't seem to be working even after specifying the height. <div style='display: table;height:500px'> <div style=' displa ...

Troubleshooting: Browser fails to update after CSSStyleRule modification using JavaScript

When making changes to CSS properties of elements using JS methods like CSSStyleSheet, insertRule, deleteRule, or CSSStyleRule.style.setProperty(), I noticed that the underlying CSS is updated but the page does not reflect these changes immediately. The c ...

Is there a way to ensure that the div is displayed on the same line?

Check out the jsfiddle link provided: http://jsfiddle.net/HE7yT/ I'm trying to align the Image, content, and Amount in the same line. How can I achieve this? The goal is to have "150" displayed on the same line as the picture. var HTML = $(' ...

Tips for incorporating Javascript in an innerHTML inline css situation

I'm just starting to learn about html5 and php. I'm curious about how to incorporate javascript into my existing code. Currently, I have data from a database displayed in an HTML table. My goal is to align the output of the last cell more toward ...

The custom classes I have created in Material UI are being overshadowed by the default classes provided by Material UI

I am trying to change the color of a TextField label to black when it is focused. I used classes in InputProps with a variable, but unfortunately, the default styling of material UI is taking precedence in chrome dev tools. Below is the code snippet. Pleas ...

Having trouble adjusting the right-hand edges of form elements when using percentage widths

Simply put, the issue is this: Why doesn't the second row of elements, with widths of 35% and 55%, take up the same amount of the page's width as the first form element which uses 90%? I often have to adjust the percentages differently dependin ...

What sets id and class apart from each other?

I understand that for id we need to use the '#' symbol and for class '.' symbol. However, I am unsure of the specific purposes of both class and id in styling CSS. Can someone provide clarification on when I should use id and when I sho ...

HTML input field that shows a hint text when selected

Is there a way to create a text box that shows a hint when clicked on? For example, you can see this feature by clicking on the address text box in the following link: ...

What is the best way to ensure that the text within Span children is properly laid out to match the width of the parent div tag?

This particular instance involves setting the width of the div tag to 200px and organizing all the span children to occupy the entire width of the parent div tag. If the first row is filled, the span tags should then flow into a second row. My attempt at ...

The method of utilizing React with Redux to display component properties

I am currently trying to include my common component in my main.js file Successfully implemented this However, when attempting to print my Redux data values in the common component, I created a method called handleClickForRedux to handle this task. Even af ...

Struggling with loading.jsx file in next js version 13.4.5?

Encountered an issue with loading components in next js 13.4.5 layout.jsx "use client"; import React, { Suspense } from "react"; import { ThemeProvider, createTheme } from "@mui/material/styles"; import CssBaseline from " ...

Struggling to keep up with responsive behavior on a page featuring vertically and horizontally centered content that spans the full height

I'm working on a full-height website and the first section needs to have both vertical and horizontal centered content. The issue I'm facing is that there's an image included, which doesn't scale responsively when the screen size change ...