Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d4d57a8

Browse files
authoredNov 17, 2020
Merge pull request #87 from tronsha/bugfix/class-for-component-on-root
Fix scoped styles for component as root node
2 parents 63eaf6d + ade361e commit d4d57a8

10 files changed

+65
-10
lines changed
 

‎src/Compiler.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,20 @@ private function preparePropertiesForInclude(array $variables, bool $isRootNode
437437
} elseif ($name === '__DATA_SCOPED_STYLE_ATTRIBUTE__') {
438438
unset($variables[$key]);
439439
if ($hasScopedStyleAttribute) {
440-
continue;
440+
foreach ($variables as $variable) {
441+
if ($variable->getName() === 'dataScopedStyleAttribute') {
442+
$variable->setValue(
443+
$variable->getValue() . ' ~ " " ~ dataScopedStyleAttribute|default(\'\')'
444+
);
445+
}
446+
}
447+
} else {
448+
$variables[] = new Property(
449+
'dataScopedStyleAttribute',
450+
'dataScopedStyleAttribute|default(\'\')',
451+
false
452+
);
441453
}
442-
$variables[] = new Property(
443-
'dataScopedStyleAttribute',
444-
'dataScopedStyleAttribute|default(\'\')',
445-
false
446-
);
447454
} elseif ($name === 'vBind') {
448455
$this->vBind = $value;
449456
unset($variables[$key]);
@@ -455,10 +462,10 @@ private function preparePropertiesForInclude(array $variables, bool $isRootNode
455462
if ($attribute === 'style') {
456463
$glue = ' ~ "; " ~ ';
457464
}
458-
$value = $values[$attribute] ?? null ? implode($glue, $values[$attribute]) : '""';
459465
if ($isRootNode) {
460-
$value = $value . $glue . $attribute . '|default(\'\')';
466+
$values[$attribute][] = $attribute . '|default(\'\')';
461467
}
468+
$value = $values[$attribute] ?? null ? implode($glue, $values[$attribute]) : '""';
462469
$variables[] = new Property($attribute, $value, false);
463470
}
464471

‎src/Models/Property.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ public function getValue(): string
5858
return $this->value;
5959
}
6060

61+
public function setValue(string $value): Property
62+
{
63+
$this->value = $value;
64+
return $this;
65+
}
66+
6167
public function isBinding(): bool
6268
{
6369
return $this->isBinding;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<style> .foo[data-v-858c11a91dcdf48f12d9b6c5ca636873] { color: purple; }</style>
2+
{% include "/templates/ChildComponent.twig" with { 'slot_default': "", 'dataScopedStyleAttribute': "data-v-858c11a91dcdf48f12d9b6c5ca636873" ~ " " ~ dataScopedStyleAttribute|default(''), 'class': "foo" ~ " " ~ class|default(''), 'style': "color: black" ~ "; " ~ style|default('') } %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<ChildComponent class="foo" style="color: black" />
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: 'ComponentWithComponentOnRoot',
8+
component: {
9+
ChildComponent,
10+
}
11+
};
12+
</script>
13+
14+
<style scoped>
15+
.foo {
16+
color: purple;
17+
}
18+
</style>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% include "/templates/ChildComponent.twig" with { 'slot_default': "", 'dataScopedStyleAttribute': dataScopedStyleAttribute|default(''), 'class': "foo" ~ " " ~ class|default(''), 'style': "color: black" ~ "; " ~ style|default('') } %}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<style> .foo[data-v-858c11a91dcdf48f12d9b6c5ca636873] { color: purple; }</style>
2+
<div data-v-858c11a91dcdf48f12d9b6c5ca636873 {{ dataScopedStyleAttribute|default('') }} class="{{ class|default('') }}" style="{{ style|default('') }}">{% include "/templates/ChildComponent.twig" with { 'slot_default': "", 'dataScopedStyleAttribute': "data-v-858c11a91dcdf48f12d9b6c5ca636873", 'class': "foo", 'style': "color: black" } %}</div>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<div>
3+
<ChildComponent class="foo" style="color: black" />
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
name: 'ComponentWithComponentOnRoot',
10+
component: {
11+
ChildComponent,
12+
}
13+
};
14+
</script>
15+
16+
<style scoped>
17+
.foo {
18+
color: purple;
19+
}
20+
</style>

‎tests/fixtures/vue-slot/include-child-component-with-component-on-root.twig

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{% set text = text|default('foo') %}
22
{% set slot_default_fallback %}{{ text }}{% endset %}
33
{% set slot_default_value %}{{ slot_default|default(slot_default_fallback) }}{% endset %}
4-
{% include "/templates/ChildComponent.twig" with { 'slot_default': slot_default_value, 'class': "" ~ " " ~ class|default(''), 'style': "" ~ "; " ~ style|default('') } %}
4+
{% include "/templates/ChildComponent.twig" with { 'slot_default': slot_default_value, 'class': class|default(''), 'style': style|default('') } %}

0 commit comments

Comments
 (0)
Please sign in to comment.