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