3
3
/** @import { ComponentContext } from '../../types' */
4
4
import { normalize_attribute } from '../../../../../../utils.js' ;
5
5
import * as b from '../../../../../utils/builders.js' ;
6
- import { build_getter } from '../../utils.js' ;
6
+ import { build_getter , create_derived } from '../../utils.js' ;
7
7
import { build_template_literal , build_update } from './utils.js' ;
8
8
9
9
/**
@@ -29,21 +29,29 @@ export function build_style_directives(
29
29
directive . value === true
30
30
? build_getter ( { name : directive . name , type : 'Identifier' } , context . state )
31
31
: build_attribute_value ( directive . value , context ) . value ;
32
+ const { has_state, has_call } = directive . metadata . expression ;
33
+
34
+ let final_value = value ;
35
+
36
+ if ( has_call ) {
37
+ const id = b . id ( state . scope . generate ( 'style_directive' ) ) ;
38
+
39
+ state . init . push ( b . const ( id , create_derived ( state , b . thunk ( value ) ) ) ) ;
40
+ final_value = b . call ( '$.get' , id ) ;
41
+ }
32
42
33
43
const update = b . stmt (
34
44
b . call (
35
45
'$.set_style' ,
36
46
element_id ,
37
47
b . literal ( directive . name ) ,
38
- value ,
48
+ final_value ,
39
49
/** @type {Expression } */ ( directive . modifiers . includes ( 'important' ) ? b . true : undefined ) ,
40
50
force_check ? b . true : undefined
41
51
)
42
52
) ;
43
53
44
- const { has_state, has_call } = directive . metadata . expression ;
45
-
46
- if ( ! is_attributes_reactive || has_call ) {
54
+ if ( ! is_attributes_reactive && has_call ) {
47
55
state . init . push ( build_update ( update ) ) ;
48
56
} else if ( is_attributes_reactive || has_state || has_call ) {
49
57
state . update . push ( update ) ;
@@ -70,11 +78,22 @@ export function build_class_directives(
70
78
const state = context . state ;
71
79
for ( const directive of class_directives ) {
72
80
const value = /** @type {Expression } */ ( context . visit ( directive . expression ) ) ;
73
- const update = b . stmt ( b . call ( '$.toggle_class' , element_id , b . literal ( directive . name ) , value ) ) ;
74
-
75
81
const { has_state, has_call } = directive . metadata . expression ;
76
82
77
- if ( ! is_attributes_reactive || has_call ) {
83
+ let final_value = value ;
84
+
85
+ if ( has_call ) {
86
+ const id = b . id ( state . scope . generate ( 'class_directive' ) ) ;
87
+
88
+ state . init . push ( b . const ( id , create_derived ( state , b . thunk ( value ) ) ) ) ;
89
+ final_value = b . call ( '$.get' , id ) ;
90
+ }
91
+
92
+ const update = b . stmt (
93
+ b . call ( '$.toggle_class' , element_id , b . literal ( directive . name ) , final_value )
94
+ ) ;
95
+
96
+ if ( ! is_attributes_reactive && has_call ) {
78
97
state . init . push ( build_update ( update ) ) ;
79
98
} else if ( is_attributes_reactive || has_state || has_call ) {
80
99
state . update . push ( update ) ;
0 commit comments