From 4bcc466d02a73e14e772dd88b3c8e07ff4d9f507 Mon Sep 17 00:00:00 2001 From: baseballyama Date: Sun, 17 Apr 2022 15:02:53 +0900 Subject: [PATCH] support --style-props for --- .../wrappers/InlineComponent/index.ts | 60 +++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts b/src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts index e6eab33fbd..e59c2ddb2a 100644 --- a/src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts +++ b/src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts @@ -423,21 +423,73 @@ export default class InlineComponentWrapper extends Wrapper { } `); + if (has_css_custom_properties) { + block.chunks.create.push(b`${css_custom_properties_wrapper} = @element("div");`); + block.chunks.hydrate.push(b`@set_style(${css_custom_properties_wrapper}, "display", "contents");`); + this.node.css_custom_properties.forEach(attr => { + const dependencies = attr.get_dependencies(); + const should_cache = attr.should_cache(); + const last = should_cache && block.get_unique_name(`${attr.name.replace(/[^a-zA-Z_$]/g, '_')}_last`); + if (should_cache) block.add_variable(last); + const value = attr.get_value(block); + const init = should_cache ? x`${last} = ${value}` : value; + + block.chunks.hydrate.push(b`@set_style(${css_custom_properties_wrapper}, "${attr.name}", ${init});`); + if (dependencies.length > 0) { + let condition = block.renderer.dirty(dependencies); + if (should_cache) condition = x`${condition} && (${last} !== (${last} = ${value}))`; + + block.chunks.update.push(b` + if (${condition}) { + @set_style(${css_custom_properties_wrapper}, "${attr.name}", ${should_cache ? last : value}); + } + `); + } + }); + } + block.chunks.create.push( b`if (${name}) @create_component(${name}.$$.fragment);` ); if (parent_nodes && this.renderer.options.hydratable) { + let nodes = parent_nodes; + if (has_css_custom_properties) { + nodes = block.get_unique_name(`${css_custom_properties_wrapper.name}_nodes`); + block.chunks.claim.push(b` + ${css_custom_properties_wrapper} = @claim_element(${parent_nodes}, "DIV", { style: true }) + var ${nodes} = @children(${css_custom_properties_wrapper}); + `); + } block.chunks.claim.push( b`if (${name}) @claim_component(${name}.$$.fragment, ${parent_nodes});` ); } - block.chunks.mount.push(b` - if (${name}) { - @mount_component(${name}, ${parent_node || '#target'}, ${parent_node ? 'null' : '#anchor'}); + if (has_css_custom_properties) { + if (parent_node) { + block.chunks.mount.push(b`@append(${parent_node}, ${css_custom_properties_wrapper})`); + if (is_head(parent_node)) { + block.chunks.destroy.push(b`@detach(${css_custom_properties_wrapper});`); + } + } else { + block.chunks.mount.push(b`@insert(#target, ${css_custom_properties_wrapper}, #anchor);`); + // TODO we eventually need to consider what happens to elements + // that belong to the same outgroup as an outroing element... + block.chunks.destroy.push(b`if (detaching) @detach(${css_custom_properties_wrapper});`); } - `); + block.chunks.mount.push(b` + if (${name}) { + @mount_component(${name}, ${css_custom_properties_wrapper}, ${parent_node ? 'null' : '#anchor'}); + } + `); + } else { + block.chunks.mount.push(b` + if (${name}) { + @mount_component(${name}, ${parent_node || '#target'}, ${parent_node ? 'null' : '#anchor'}); + } + `); + } const anchor = this.get_or_create_anchor(block, parent_node, parent_nodes); const update_mount_node = this.get_update_mount_node(anchor);