|
2 | 2 | * Imported ionic string functions for SCSS
|
3 | 3 | * ----------------------------------------------------------------------------
|
4 | 4 | * Extracted from
|
5 |
| - * https://github.com/ionic-team/ionic-framework/blob/master/core/src/themes/ionic.functions.string.scss |
| 5 | + * https://github.com/ionic-team/ionic-framework/blob/main/core/src/themes/ionic.functions.string.scss |
6 | 6 | */
|
7 | 7 |
|
8 | 8 |
|
|
93 | 93 | // Add Root Selector
|
94 | 94 | // --------------------------------------------------------------------------------
|
95 | 95 | // Adds a root selector using host based on the selector passed
|
| 96 | +// $root: The selector that needs to be updated to include the $addHostSelector. |
| 97 | +// - Example: ion-button |
| 98 | +// $addHostSelector: The selector that is used to add the host to the $root selector. |
| 99 | +// - Example: [dir=rtl] |
| 100 | +// $useHostContext: Whether to use host-context or not. Defaults to true. |
96 | 101 | // --------------------------------------------------------------------------------
|
97 | 102 |
|
98 |
| -@function add-root-selector($root, $addHostSelector) { |
| 103 | + |
| 104 | +@function add-root-selector($root, $addHostSelector, $useHostContext: true) { |
99 | 105 | $selectors: str-split($root, ",");
|
100 | 106 |
|
101 | 107 | $list: ();
|
102 | 108 |
|
103 | 109 | @each $selector in $selectors {
|
104 | 110 | // If the selector contains :host( it means it is targeting a class on the host
|
105 | 111 | // element so we need to change how we target it:
|
| 112 | + // Example with `useHostContext=true` |
106 | 113 | // @include add-root-selector(":host(.fixed)", "[dir=rtl]")
|
107 | 114 | // --> :host-context([dir=rtl]):host(.fixed)
|
108 | 115 | // --> :host-context([dir=rtl]).fixed
|
| 116 | + // --- |
| 117 | + // Example with `useHostContext=false` |
| 118 | + // @include add-root(":host(.fixed)", ":dir(rtl)", false) |
| 119 | + // --> :host(.fixed:dir(rtl)) |
109 | 120 | @if str-contains($selector, ":host(") {
|
110 |
| - // @include add-root-selector(":host(.fixed)", "[dir=rtl]") |
111 |
| - // --> :host-context([dir=rtl]):host(.fixed) |
112 |
| - $shadow-element: str-replace($selector, ":host(", ":host-context(#{$addHostSelector}):host("); |
113 |
| - $list: append($list, $shadow-element, comma); |
| 121 | + @if $useHostContext { |
| 122 | + // @include add-root-selector(":host(.fixed)", "[dir=rtl]") |
| 123 | + // --> :host-context([dir=rtl]):host(.fixed) |
| 124 | + $shadow-element: str-replace($selector, ":host(", ":host-context(#{$addHostSelector}):host("); |
| 125 | + $list: append($list, $shadow-element, comma); |
| 126 | + } |
114 | 127 |
|
115 | 128 | $new-element: ();
|
116 | 129 | $elements: str-split($selector, " ");
|
|
125 | 138 | $scoped-element: str-replace($scoped-element, ")", "");
|
126 | 139 | $scoped-element: str-replace($scoped-element, ":host(", "");
|
127 | 140 |
|
128 |
| - // Add the class back inside of host with the rtl selector: |
129 |
| - // .fixed -> :host-context([dir=rtl]).fixed |
130 |
| - $scoped-element: str-replace($scoped-element, $scoped-element, ":host-context(#{$addHostSelector})#{$scoped-element}"); |
| 141 | + // Add the class back inside of host with the addHostSelector: |
| 142 | + @if $useHostContext { |
| 143 | + // .fixed -> :host-context([dir=rtl]).fixed |
| 144 | + $scoped-element: str-replace($scoped-element, $scoped-element, ":host-context(#{$addHostSelector})#{$scoped-element}"); |
| 145 | + } @else { |
| 146 | + // .fixed -> :host(.fixed:dir(rtl)) |
| 147 | + $scoped-element: str-replace($scoped-element, $scoped-element, ":host(#{$scoped-element}#{$addHostSelector})"); |
| 148 | + } |
131 | 149 |
|
132 | 150 | // @include add-root-selector(":host(.fixed)", "[dir=rtl]")
|
133 | 151 | // --> :host-context([dir=rtl]).fixed
|
| 152 | + // @include add-root(":host(.fixed)", ":dir(rtl)", false) |
| 153 | + // --> :host(.fixed:dir(rtl)) |
134 | 154 | $new-element: append($new-element, $scoped-element, space);
|
135 | 155 | } @else {
|
136 |
| - // Add back any selectors that followed the host after transforming the |
137 |
| - // first selector: |
138 |
| - // :host(.fixed) ::slotted(ion-icon) |
| 156 | + // Add back any selectors that followed the host |
| 157 | + // after transforming the first selector: |
| 158 | + // @include add-root-selector(":host(.fixed) ::slotted(ion-icon)", "[dir=rtl]") |
139 | 159 | // --> :host-context([dir=rtl]):host(.fixed) ::slotted(ion-icon)
|
140 | 160 | // --> :host-context([dir=rtl]).fixed ::slotted(ion-icon)
|
| 161 | + // @include add-root(":host(.fixed) ::slotted(ion-icon)", ":dir(rtl)", false) |
| 162 | + // --> :host(.fixed:dir(rtl)) ::slotted(ion-icon) |
141 | 163 | $new-element: append($new-element, $element, space);
|
142 | 164 | }
|
143 | 165 | }
|
|
148 | 170 | // element so we can change it to look for host-context
|
149 | 171 | // @include add-root-selector(":host", "[dir=rtl]")
|
150 | 172 | // --> :host-context([dir=rtl])
|
151 |
| - // --> :host:dir(rtl) |
| 173 | + // @include add-root(":host", ":dir(rtl)", false) |
| 174 | + // --> :host(:dir(rtl)) |
152 | 175 | } @else if str-contains($selector, ":host") {
|
153 | 176 | $new-element: ();
|
154 | 177 | $elements: str-split($selector, " ");
|
155 | 178 |
|
156 | 179 | @each $element in $elements {
|
157 | 180 | @if str-contains($element, ":host") {
|
| 181 | + $updated-element: ''; |
| 182 | + |
158 | 183 | // Replace the :host with the addHostSelector:
|
159 |
| - // :host -> :host-context([dir=rtl]) |
160 |
| - $updated-element: str-replace($element, ":host", ":host-context(#{$addHostSelector})"); |
| 184 | + @if $useHostContext { |
| 185 | + // :host -> :host-context([dir=rtl]) |
| 186 | + $updated-element: str-replace($element, ":host", ":host-context(#{$addHostSelector})"); |
| 187 | + } @else { |
| 188 | + // :host -> :host(:dir(rtl)) |
| 189 | + $updated-element: str-replace($element, ":host", ":host(#{$addHostSelector})"); |
| 190 | + } |
161 | 191 |
|
162 | 192 | // Add the final selector after all transformations:
|
163 |
| - // :host -> :host-context([dir=rtl]) |
| 193 | + // @include add-root-selector(":host", "[dir=rtl]") |
| 194 | + // --> :host-context([dir=rtl]) |
| 195 | + // @include add-root(":host", ":dir(rtl)", false) |
| 196 | + // --> :host(:dir(rtl)) |
164 | 197 | $new-element: append($new-element, $updated-element, space);
|
165 | 198 | } @else {
|
166 |
| - // Add back any selectors that followed the host after transforming the |
167 |
| - // first selector: |
168 |
| - // :host ::slotted(ion-icon) -> :host-context([dir=rtl]) ::slotted(ion-icon) |
| 199 | + // Add back any selectors that followed the host |
| 200 | + // after transforming the first selector: |
| 201 | + // @include add-root-selector(":host ::slotted(ion-icon)", "[dir=rtl]") |
| 202 | + // --> :host-context([dir=rtl]) ::slotted(ion-icon) |
| 203 | + // @include add-root(":host ::slotted(ion-icon)", ":dir(rtl)", false) |
| 204 | + // --> :host(:dir(rtl)) ::slotted(ion-icon) |
169 | 205 | $new-element: append($new-element, $element, space);
|
170 | 206 | }
|
171 | 207 | }
|
|
176 | 212 | // @include add-root-selector("ion-component", "[dir=rtl]")
|
177 | 213 | // --> :host-context([dir=rtl]) ion-component
|
178 | 214 | // --> [dir=rtl] ion-component
|
| 215 | + // @include add-root("ion-component", ":dir(rtl)", false) |
| 216 | + // --> ion-component:dir(rtl) |
179 | 217 | } @else {
|
180 |
| - $list: append($list, "#{$addHostSelector} #{$selector}", comma); |
181 |
| - $list: append($list, ":host-context(#{$addHostSelector}) #{$selector}", comma); |
| 218 | + @if ($useHostContext) { |
| 219 | + $list: append($list, ":host-context(#{$addHostSelector}) #{$selector}", comma); |
| 220 | + $list: append($list, "#{$addHostSelector} #{$selector}", comma); |
| 221 | + } @else { |
| 222 | + $list: append($list, "#{$selector}#{$addHostSelector}", comma); |
| 223 | + } |
182 | 224 | }
|
183 | 225 | }
|
184 | 226 |
|
|
0 commit comments