@unsass/selector 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,26 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
4
4
 
5
+ ## [1.2.0](https://github.com/unsass/selector/compare/v1.1.0...v1.2.0) (2023-03-20)
6
+
7
+
8
+ ### Features
9
+
10
+ * allow `&` nesting selector ([20d12a2](https://github.com/unsass/selector/commit/20d12a2302e8a8c31a60e19094bbeb2aa16af1b1))
11
+
12
+
13
+ ### Code Refactoring
14
+
15
+ * add better error messages ([6ae5295](https://github.com/unsass/selector/commit/6ae529558dc230a0cdf7677b7fa226e2270e5436))
16
+ * removed unneeded sass import ([b817859](https://github.com/unsass/selector/commit/b817859233c967a78f0fba7752cbaedda112abff))
17
+
18
+ ## [1.1.0](https://github.com/unsass/selector/compare/v1.0.0...v1.1.0) (2023-03-20)
19
+
20
+
21
+ ### Features
22
+
23
+ * use `@unsass/string` instead of specific function ([af2dd21](https://github.com/unsass/selector/commit/af2dd217a4acea2e2734037749b010f2d73f02ff))
24
+
5
25
  ## 1.0.0 (2022-08-31)
6
26
 
7
27
 
package/README.md CHANGED
@@ -13,3 +13,131 @@ Sass functions and mixins to manage CSS selectors.
13
13
  ```shell
14
14
  npm install @unsass/selector
15
15
  ```
16
+
17
+ ## Usage
18
+
19
+ ```scss
20
+ @use "@unsass/selector";
21
+
22
+ @include selector.create("foo", "md") {
23
+ color: darkcyan;
24
+ }
25
+ ```
26
+
27
+ ### Result
28
+
29
+ ```css
30
+ .md\:foo {
31
+ color: darkcyan;
32
+ }
33
+ ```
34
+
35
+ ## API
36
+
37
+ | Mixin | Description |
38
+ |-----------------------------------------------------------------------------------------|------------------------------------------------------------|
39
+ | `create($selector, $scope, $separator, $suffix, $pseudo-class, $pseudo-element, $root)` | Sets new CSS selector with class scope and pseudo options. |
40
+
41
+ ### `$separator`
42
+
43
+ Define your own scope separator character.
44
+
45
+ ```scss
46
+ @use "@unsass/selector";
47
+
48
+ @include selector.create("foo", "md", "@") {
49
+ color: darkcyan;
50
+ }
51
+ ```
52
+
53
+ ### Result
54
+
55
+ ```css
56
+ .md\@foo {
57
+ color: darkcyan;
58
+ }
59
+ ```
60
+
61
+ ### `$suffix`
62
+
63
+ Define the scope value has a prefix on selector.
64
+
65
+ ```scss
66
+ @use "@unsass/selector";
67
+
68
+ @include selector.create("foo", "md", $suffix: true) {
69
+ color: darkcyan;
70
+ }
71
+ ```
72
+
73
+ ### Result
74
+
75
+ ```css
76
+ .foo\:md {
77
+ color: darkcyan;
78
+ }
79
+ ```
80
+
81
+ ### `$pseudo-class`
82
+
83
+ Define the pseudo class suffix.
84
+
85
+ ```scss
86
+ @use "@unsass/selector";
87
+
88
+ @include selector.create("foo", "hover", $pseudo-class: "hover") {
89
+ color: darkcyan;
90
+ }
91
+ ```
92
+
93
+ ### Result
94
+
95
+ ```css
96
+ .hover\:foo:hover {
97
+ color: darkcyan;
98
+ }
99
+ ```
100
+
101
+ ### `$pseudo-element`
102
+
103
+ Define the pseudo element suffix.
104
+
105
+ ```scss
106
+ @use "@unsass/selector";
107
+
108
+ @include selector.create("foo", "before", $pseudo-element: "before") {
109
+ color: darkcyan;
110
+ }
111
+ ```
112
+
113
+ ### Result
114
+
115
+ ```css
116
+ .before\:foo::before {
117
+ color: darkcyan;
118
+ }
119
+ ```
120
+
121
+ ### `$root`
122
+
123
+ Wrap the selector with `@at-root` rule before code output.
124
+
125
+ ```scss
126
+ @use "@unsass/selector";
127
+
128
+ .foo {
129
+ .bar {
130
+ @include selector.create(&, "md", $root: true) {
131
+ color: darkcyan;
132
+ }
133
+ }
134
+ }
135
+ ```
136
+
137
+ ### Result
138
+
139
+ ```css
140
+ .md\:foo .bar {
141
+ color: darkcyan;
142
+ }
143
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unsass/selector",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "Sass functions and mixins to manage CSS selectors.",
5
5
  "scripts": {
6
6
  "lint:css": "stylelint --fix \"**/*.scss\"",
@@ -10,26 +10,29 @@
10
10
  "release": "semantic-release"
11
11
  },
12
12
  "engines": {
13
- "node": ">=14.0.0",
13
+ "node": ">=16.0.0",
14
14
  "npm": ">=8.0.0"
15
15
  },
16
16
  "sass": "index.scss",
17
17
  "devDependencies": {
18
- "@commitlint/cli": "^17.0.3",
19
- "@commitlint/config-conventional": "^17.0.3",
20
- "@semantic-release/changelog": "^6.0.1",
18
+ "@commitlint/cli": "^17.4.4",
19
+ "@commitlint/config-conventional": "^17.4.4",
20
+ "@semantic-release/changelog": "^6.0.2",
21
21
  "@semantic-release/git": "^10.0.1",
22
22
  "glob": "^7.2.0",
23
- "husky": "^8.0.1",
24
- "jest": "^28.1.3",
23
+ "husky": "^8.0.3",
24
+ "jest": "^29.5.0",
25
+ "jest-environment-node-single-context": "^29.0.0",
26
+ "jest-junit": "^15.0.0",
25
27
  "npm-run-all": "^4.1.5",
26
- "sass-true": "^6.1.0",
27
- "semantic-release": "^19.0.3",
28
- "stylelint": "^14.10.0",
29
- "stylelint-config-unsass": "^1.0.0"
28
+ "sass-true": "^7.0.0",
29
+ "semantic-release": "^20.1.3",
30
+ "stylelint": "^15.3.0",
31
+ "stylelint-config-unsass": "^1.0.1"
30
32
  },
31
33
  "dependencies": {
32
- "@unsass/utilities": "^1.3.1",
34
+ "@unsass/string": "^1.4.2",
35
+ "@unsass/types": "^1.0.2",
33
36
  "sass": "^1.53.0"
34
37
  },
35
38
  "keywords": [
@@ -59,29 +59,3 @@
59
59
  @function to-class($selector) {
60
60
  @return ".#{$selector}";
61
61
  }
62
-
63
- ///
64
- /// Number.
65
- ///
66
- @function number($value) {
67
- @if meta.type-of($value) == "number" {
68
- @return $value;
69
- } @else if meta.type-of($value) != "string" {
70
- @error "Invalid string type for `#{$value}` value. Use string type instead.";
71
- }
72
-
73
- $numbers: (
74
- "0": 0,
75
- "1": 1,
76
- "2": 2,
77
- "3": 3,
78
- "4": 4,
79
- "5": 5,
80
- "6": 6,
81
- "7": 7,
82
- "8": 8,
83
- "9": 9
84
- );
85
-
86
- @return map.get($numbers, $value);
87
- }
package/src/_mixins.scss CHANGED
@@ -2,15 +2,15 @@
2
2
  // MIXINS //
3
3
  // ============================================================================================= //
4
4
 
5
- @use "sass:meta";
5
+ @use "sass:list";
6
6
  @use "sass:selector";
7
- @use "sass:string";
8
- @use "@unsass/utilities";
7
+ @use "@unsass/string";
8
+ @use "@unsass/types" as type;
9
9
  @use "./functions";
10
10
 
11
11
  ///
12
12
  /// @example - scss
13
- /// @include create("foo") {
13
+ /// @include selector.create("foo") {
14
14
  /// color: darkcyan;
15
15
  /// }
16
16
  ///
@@ -20,7 +20,7 @@
20
20
  /// }
21
21
  ///
22
22
  /// @example - scss
23
- /// @include create("foo", "md") {
23
+ /// @include selector.create("foo", "md") {
24
24
  /// color: darkcyan;
25
25
  /// }
26
26
  ///
@@ -30,7 +30,7 @@
30
30
  /// }
31
31
  ///
32
32
  /// @example - scss
33
- /// @include create("foo", $pseudo-class: "hover") {
33
+ /// @include selector.create("foo", $pseudo-class: "hover") {
34
34
  /// color: darkcyan;
35
35
  /// }
36
36
  ///
@@ -40,7 +40,7 @@
40
40
  /// }
41
41
  ///
42
42
  /// @example - scss
43
- /// @include create("foo", $pseudo-element: "before") {
43
+ /// @include selector.create("foo", $pseudo-element: "before") {
44
44
  /// color: darkcyan;
45
45
  /// }
46
46
  ///
@@ -58,16 +58,17 @@
58
58
  ///
59
59
  /// @see {function} functions.pseudo-class
60
60
  /// @see {function} functions.pseudo-element
61
- /// @see {function} functions.number
62
- /// @see {function} utilities.string-replace
63
61
  ///
64
62
  /// @access public
65
63
  ///
66
- @mixin create($selector, $scope: null, $separator: ":", $suffix: false, $pseudo-class: null, $pseudo-element: null) {
67
- @if ($pseudo-class and $suffix) or ($pseudo-element and $suffix) {
68
- @error "You cannot add a suffix with a selector that already has a pseudo-class or a pseudo-element."
64
+ @mixin create($selector, $scope: null, $separator: ":", $suffix: false, $pseudo-class: null, $pseudo-element: null, $root: false) {
65
+ @if $pseudo-class and $suffix {
66
+ @error "You cannot add a suffix with a selector that already has a pseudo-class."
67
+ } @else if $pseudo-element and $suffix {
68
+ @error "You cannot add a suffix with a selector that already has a pseudo-element."
69
69
  }
70
70
 
71
+ $selector: if(type.is-list($selector), "#{list.nth($selector, 1)}", $selector);
71
72
  $class-selector: string.slice($selector, 1, 1) == ".";
72
73
 
73
74
  @if $class-selector {
@@ -80,9 +81,7 @@
80
81
 
81
82
  @if $pseudo-class {
82
83
  $selector: functions.pseudo-class($selector, $pseudo-class);
83
- }
84
-
85
- @if $pseudo-element {
84
+ } @else if $pseudo-element {
86
85
  $selector: functions.pseudo-element($selector, $pseudo-element);
87
86
  }
88
87
 
@@ -91,8 +90,8 @@
91
90
  ///
92
91
 
93
92
  @if $scope and not $suffix {
94
- @if meta.type-of(functions.number(string.slice($scope, 1, $end-at: 1))) == "number" {
95
- $scope: utilities.string-replace(\3 #{string.unquote($scope)}, " ");
93
+ @if type.is-number(string.to-number(string.slice($scope, 1, $end-at: 1))) {
94
+ $scope: string.replace(\3 #{string.unquote($scope)}, " ");
96
95
  }
97
96
 
98
97
  $selector: selector.append("#{$scope}\\#{$separator}", $selector);
@@ -106,8 +105,16 @@
106
105
  /// Set selector.
107
106
  ///
108
107
 
109
- #{functions.to-class($selector)} {
110
- @content;
108
+ $selector: functions.to-class($selector);
109
+
110
+ @if $root {
111
+ @at-root #{$selector} {
112
+ @content;
113
+ }
114
+ } @else {
115
+ #{$selector} {
116
+ @content;
117
+ }
111
118
  }
112
119
  }
113
120