Git Product home page Git Product logo

insert-selector's Introduction

insert-selector

SCSS mixin for adding a selector into a nested list.

It automatically adds class, ID, attribute, and pseudo selectors to the end of the target. It adds elements to the front.

Requirements

Dart Sass >=1.23.0 for @use module syntax

Install

npm install insert-selector

yarn add insert-selector

Usage

Import the mixin at the top of your .scss file.

If using module syntax (it's the future)(and also the default), and you can use the package import:

@use "insert-selector" as *;

// With Dart Sass 1.71.0, you can use the pkg: importer
@use "pkg:insert-selector" as *;

// The old way, something like this
@use "../../../../node_modules/insert-selector/insert-selector" as *;

You can also use a load path to make it cleaner. (Load path in CLI docs)

If using @import syntax:

@import "insert-selector-import";
/// @param {string} $target
///   the target element in the tree
/// @param {string} $insert
///   the element or modifier you want to insert
/// @param {boolean} $child, default false
///   if true, the $insert is a separate child element in the selector tree
/// @return {string}
///   full new element tree

@include insert-selector("#target", ".insert");
@include insert-selector("#target", ".insert", true);

Examples

.product-info {
  .row-fluid {
    .price-info {
      display: grid;

      .buy-info {
        grid-column: 1;

        @include insert-selector(".row-fluid", ".active") {
          grid-column: 3;
        }

        @include insert-selector(".row-fluid", ".outOfStock", true) {
          grid-column: 2;
        }
      }
    }
  }
}

compiles to

.product-info .row-fluid .price-info {
  display: grid;
}
.product-info .row-fluid .price-info .buy-info {
  grid-column: 1;
}
.product-info .row-fluid.active .price-info .buy-info {
  grid-column: 3;
}
.product-info .row-fluid .outOfStock .price-info .buy-info {
  grid-column: 2;
}

Why would I want this?

You're probably on a legacy system that has difficult to navigate element trees for selectors. Ideally, the markup doesn't look like this, but sometimes it's out of your hands. Hopefully this helps!

This also acts like a parent selector, giving you access to change the parent from the nested child selector.

insert-selector's People

Contributors

garymazzeo avatar gmazzeozing avatar

Stargazers

 avatar  avatar  avatar

insert-selector's Issues

If target fully shows up before intended target, it will insert too early

Specifically, this happened with trying to target an a tag where there was a header previous to it, so the a in header was targeted instead.

Possible solution:
Make sure target has a space on at least one side (though, if there's an anchor target to start the whole thing, and it has a class tacked on it, it would not have any spaces) SO
Make sure target has a space or selector character (# . : [) on at least one side.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.