Skip to main content
Create new
Introduction
Reactivity
Props
Logic
Events
Bindings
Lifecycle
Stores
Motion
Transitions
Animations
Easing
SVG
Actions
Classes
Component composition
Context API
Special elements
Module context
Debugging
7GUIs
Miscellaneous
App.svelte
<script>
import setFocus from "@svackages/set-focus@1.0.1";
import { fade } from "svelte/transition";

let showOverlay = false;
</script>

<h1>svelte action set-focus</h1>
<button on:click={() => showOverlay = true}>Open overlay</button>

{#if showOverlay}
<div
transition:fade
use:setFocus>
<h2>I am an overlay</h2>
<button on:click={() => showOverlay = false}>Close overlay</button>
</div>
{/if}


<style>
*:focus {
outline-color: red;
outline-width: 5px;
}

div {
border: 1px dotted dodgerblue;
}
</style>
bundling https://unpkg.com/clsx@2.1.1/dist/clsx.mjs
import 'svelte/internal/disclose-version';
import 'svelte/internal/flags/legacy';
import * as $ from 'svelte/internal/client';
import setFocus from "@svackages/set-focus@1.0.1";
import { fade } from "svelte/transition";

var root_1 = $.template(`<div class="svelte-1xrx0jc"><h2 class="svelte-1xrx0jc">I am an overlay</h2> <button class="svelte-1xrx0jc">Close overlay</button></div>`);
var root = $.template(`<h1 class="svelte-1xrx0jc">svelte action set-focus</h1> <button class="svelte-1xrx0jc">Open overlay</button> <!>`, 1);

export default function App($$anchor) {
let showOverlay = $.mutable_state(false);
var fragment = root();
var button = $.sibling($.first_child(fragment), 2);
var node = $.sibling(button, 2);

{
var consequent = ($$anchor) => {
var div = root_1();
var button_1 = $.sibling($.child(div), 2);

$.reset(div);
$.action(div, ($$node) => setFocus?.($$node));
$.event('click', button_1, () => $.set(showOverlay, false));
$.transition(3, div, () => fade);
$.append($$anchor, div);
};

$.if(node, ($$render) => {
if ($.get(showOverlay)) $$render(consequent);
});
}

$.event('click', button, () => $.set(showOverlay, true));
$.append($$anchor, fragment);
}
result = svelte.compile(source, {
generate: ,
});

.svelte-1xrx0jc:focus {
outline-color: red;
outline-width: 5px;
}

div.svelte-1xrx0jc {
border: 1px dotted dodgerblue;
}

		
			
				
  • Root {
    • css: StyleSheet {...}
      • type: "StyleSheet"
      • start: 452
      • end: 600
      • attributes: []
      • children: [...] (2)
        • Rule {...}
          • type: "Rule"
          • prelude: SelectorList {...}
            • type: "SelectorList"
            • start: 464
            • end: 471
            • children: [...] (1)
              • ComplexSelector {...}
                • type: "ComplexSelector"
                • start: 464
                • end: 471
                • children: [...] (1)
                  • RelativeSelector {...}
                    • type: "RelativeSelector"
                    • combinator: null
                    • selectors: [...] (2)
                      • TypeSelector {...}
                        • type: "TypeSelector"
                        • name: "*"
                        • start: 464
                        • end: 465
                        }
                      • PseudoClassSelector {...}
                        • type: "PseudoClassSelector"
                        • name: "focus"
                        • args: null
                        • start: 465
                        • end: 471
                        }
                      ]
                    • start: 464
                    • end: 471
                    }
                  ]
                }
              ]
            }
          • block: Block {...}
            • type: "Block"
            • start: 472
            • end: 535
            • children: [...] (2)
              • Declaration {...}
                • type: "Declaration"
                • start: 482
                • end: 500
                • property: "outline-color"
                • value: "red"
                }
              • Declaration {...}
                • type: "Declaration"
                • start: 510
                • end: 528
                • property: "outline-width"
                • value: "5px"
                }
              ]
            }
          • start: 464
          • end: 535
          }
        • Rule {...}
          • type: "Rule"
          • prelude: SelectorList {...}
            • type: "SelectorList"
            • start: 541
            • end: 544
            • children: [...] (1)
              • ComplexSelector {...}
                • type: "ComplexSelector"
                • start: 541
                • end: 544
                • children: [...] (1)
                  • RelativeSelector {...}
                    • type: "RelativeSelector"
                    • combinator: null
                    • selectors: [...] (1)
                      • TypeSelector {...}
                        • type: "TypeSelector"
                        • name: "div"
                        • start: 541
                        • end: 544
                        }
                      ]
                    • start: 541
                    • end: 544
                    }
                  ]
                }
              ]
            }
          • block: Block {...}
            • type: "Block"
            • start: 545
            • end: 591
            • children: [...] (1)
              • Declaration {...}
                • type: "Declaration"
                • start: 555
                • end: 584
                • property: "border"
                • value: "1px dotted dodgerblue"
                }
              ]
            }
          • start: 541
          • end: 591
          }
        ]
      • content: {...}
        • start: 459
        • end: 592
        • styles: "\n *:focus {\n outline-color: red;\n outline-width: 5px;\n }\n\n div {\n border: 1px dotted dodgerblue;\n }\n"
        • comment: null
        }
      }
    • js: []
    • start: 151
    • end: 449
    • type: "Root"
    • fragment: Fragment {...}
      • type: "Fragment"
      • nodes: [...] (7)
        • Text {...}
          • type: "Text"
          • start: 149
          • end: 151
          • raw: "\n\n"
          • data: "\n\n"
          }
        • RegularElement {...}
          • type: "RegularElement"
          • start: 151
          • end: 183
          • name: "h1"
          • attributes: [...] (1)
            • Attribute {...}
              • type: "Attribute"
              • start: -1
              • end: -1
              • name: "class"
              • value: [...] (1)
                • Text {...}
                  • type: "Text"
                  • data: ""
                  • raw: ""
                  • start: -1
                  • end: -1
                  }
                ]
              }
            ]
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (1)
              • Text {...}
                • type: "Text"
                • start: 155
                • end: 178
                • raw: "svelte action set-focus"
                • data: "svelte action set-focus"
                }
              ]
            }
          }
        • Text {...}
          • type: "Text"
          • start: 183
          • end: 184
          • raw: " "
          • data: " "
          }
        • RegularElement {...}
          • type: "RegularElement"
          • start: 184
          • end: 249
          • name: "button"
          • attributes: [...] (2)
            • OnDirective {...}
              • start: 192
              • end: 227
              • type: "OnDirective"
              • name: "click"
              • expression: ArrowFunctionExpression {...}
                • type: "ArrowFunctionExpression"
                • start: 202
                • end: 226
                • loc: {...}
                  • start: {...}
                    • line: 9
                    • column: 18
                    }
                  • end: {...}
                    • line: 9
                    • column: 42
                    }
                  }
                • id: null
                • expression: true
                • generator: false
                • async: false
                • params: []
                • body: AssignmentExpression {...}
                  • type: "AssignmentExpression"
                  • start: 208
                  • end: 226
                  • loc: {...}
                    • start: {...}
                      • line: 9
                      • column: 24
                      }
                    • end: {...}
                      • line: 9
                      • column: 42
                      }
                    }
                  • operator: "="
                  • left: Identifier {...}
                    • type: "Identifier"
                    • start: 208
                    • end: 219
                    • loc: {...}
                      • start: {...}
                        • line: 9
                        • column: 24
                        }
                      • end: {...}
                        • line: 9
                        • column: 35
                        }
                      }
                    • name: "showOverlay"
                    }
                  • right: Literal {...}
                    • type: "Literal"
                    • start: 222
                    • end: 226
                    • loc: {...}
                      • start: {...}
                        • line: 9
                        • column: 38
                        }
                      • end: {...}
                        • line: 9
                        • column: 42
                        }
                      }
                    • value: true
                    • raw: "true"
                    }
                  }
                }
              • modifiers: []
              }
            • Attribute {...}
              • type: "Attribute"
              • start: -1
              • end: -1
              • name: "class"
              • value: [...] (1)
                • Text {...}
                  • type: "Text"
                  • data: ""
                  • raw: ""
                  • start: -1
                  • end: -1
                  }
                ]
              }
            ]
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (1)
              • Text {...}
                • type: "Text"
                • start: 228
                • end: 240
                • raw: "Open overlay"
                • data: "Open overlay"
                }
              ]
            }
          }
        • Text {...}
          • type: "Text"
          • start: 249
          • end: 251
          • raw: " "
          • data: " "
          }
        • IfBlock {...}
          • type: "IfBlock"
          • elseif: false
          • start: 251
          • end: 449
          • test: Identifier {...}
            • type: "Identifier"
            • start: 256
            • end: 267
            • loc: {...}
              • start: {...}
                • line: 11
                • column: 5
                }
              • end: {...}
                • line: 11
                • column: 16
                }
              }
            • name: "showOverlay"
            }
          • consequent: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (3)
              • Text {...}
                • type: "Text"
                • start: 268
                • end: 273
                • raw: "\n "
                • data: "\n "
                }
              • RegularElement {...}
                • type: "RegularElement"
                • start: 273
                • end: 443
                • name: "div"
                • attributes: [...] (3)
                  • TransitionDirective {...}
                    • start: 286
                    • end: 301
                    • type: "TransitionDirective"
                    • name: "fade"
                    • expression: null
                    • modifiers: []
                    • intro: true
                    • outro: true
                    }
                  • UseDirective {...}
                    • start: 310
                    • end: 322
                    • type: "UseDirective"
                    • name: "setFocus"
                    • expression: null
                    • modifiers: []
                    }
                  • Attribute {...}
                    • type: "Attribute"
                    • start: -1
                    • end: -1
                    • name: "class"
                    • value: [...] (1)
                      • Text {...}
                        • type: "Text"
                        • data: ""
                        • raw: ""
                        • start: -1
                        • end: -1
                        }
                      ]
                    }
                  ]
                • fragment: Fragment {...}
                  • type: "Fragment"
                  • nodes: [...] (5)
                    • Text {...}
                      • type: "Text"
                      • start: 323
                      • end: 332
                      • raw: "\n "
                      • data: "\n "
                      }
                    • RegularElement {...}
                      • type: "RegularElement"
                      • start: 332
                      • end: 356
                      • name: "h2"
                      • attributes: [...] (1)
                        • Attribute {...}
                          • type: "Attribute"
                          • start: -1
                          • end: -1
                          • name: "class"
                          • value: [...] (1)
                            • Text {...}
                              • type: "Text"
                              • data: ""
                              • raw: ""
                              • start: -1
                              • end: -1
                              }
                            ]
                          }
                        ]
                      • fragment: Fragment {...}
                        • type: "Fragment"
                        • nodes: [...] (1)
                          • Text {...}
                            • type: "Text"
                            • start: 336
                            • end: 351
                            • raw: "I am an overlay"
                            • data: "I am an overlay"
                            }
                          ]
                        }
                      }
                    • Text {...}
                      • type: "Text"
                      • start: 356
                      • end: 365
                      • raw: " "
                      • data: " "
                      }
                    • RegularElement {...}
                      • type: "RegularElement"
                      • start: 365
                      • end: 432
                      • name: "button"
                      • attributes: [...] (2)
                        • OnDirective {...}
                          • start: 373
                          • end: 409
                          • type: "OnDirective"
                          • name: "click"
                          • expression: ArrowFunctionExpression {...}
                            • type: "ArrowFunctionExpression"
                            • start: 383
                            • end: 408
                            • loc: {...}
                              • start: {...}
                                • line: 16
                                • column: 26
                                }
                              • end: {...}
                                • line: 16
                                • column: 51
                                }
                              }
                            • id: null
                            • expression: true
                            • generator: false
                            • async: false
                            • params: []
                            • body: AssignmentExpression {...}
                              • type: "AssignmentExpression"
                              • start: 389
                              • end: 408
                              • loc: {...}
                                • start: {...}
                                  • line: 16
                                  • column: 32
                                  }
                                • end: {...}
                                  • line: 16
                                  • column: 51
                                  }
                                }
                              • operator: "="
                              • left: Identifier {...}
                                • type: "Identifier"
                                • start: 389
                                • end: 400
                                • loc: {...}
                                  • start: {...}
                                    • line: 16
                                    • column: 32
                                    }
                                  • end: {...}
                                    • line: 16
                                    • column: 43
                                    }
                                  }
                                • name: "showOverlay"
                                }
                              • right: Literal {...}
                                • type: "Literal"
                                • start: 403
                                • end: 408
                                • loc: {...}
                                  • start: {...}
                                    • line: 16
                                    • column: 46
                                    }
                                  • end: {...}
                                    • line: 16
                                    • column: 51
                                    }
                                  }
                                • value: false
                                • raw: "false"
                                }
                              }
                            }
                          • modifiers: []
                          }
                        • Attribute {...}
                          • type: "Attribute"
                          • start: -1
                          • end: -1
                          • name: "class"
                          • value: [...] (1)
                            • Text {...}
                              • type: "Text"
                              • data: ""
                              • raw: ""
                              • start: -1
                              • end: -1
                              }
                            ]
                          }
                        ]
                      • fragment: Fragment {...}
                        • type: "Fragment"
                        • nodes: [...] (1)
                          • Text {...}
                            • type: "Text"
                            • start: 410
                            • end: 423
                            • raw: "Close overlay"
                            • data: "Close overlay"
                            }
                          ]
                        }
                      }
                    • Text {...}
                      • type: "Text"
                      • start: 432
                      • end: 437
                      • raw: "\n "
                      • data: "\n "
                      }
                    ]
                  }
                }
              • Text {...}
                • type: "Text"
                • start: 443
                • end: 444
                • raw: "\n"
                • data: "\n"
                }
              ]
            }
          • alternate: null
          }
        • Text {...}
          • type: "Text"
          • start: 449
          • end: 452
          • raw: "\n\n\n"
          • data: "\n\n\n"
          }
        ]
      }
    • options: null
    • instance: Script {...}
      • type: "Script"
      • start: 0
      • end: 149
      • context: "default"
      • content: Program {...}
        • type: "Program"
        • start: 8
        • end: 140
        • loc: {...}
          • start: {...}
            • line: 1
            • column: 0
            }
          • end: {...}
            • line: 6
            • column: 0
            }
          }
        • body: [...] (3)
          • ImportDeclaration {...}
            • type: "ImportDeclaration"
            • start: 13
            • end: 63
            • loc: {...}
              • start: {...}
                • line: 2
                • column: 4
                }
              • end: {...}
                • line: 2
                • column: 54
                }
              }
            • specifiers: [...] (1)
              • ImportDefaultSpecifier {...}
                • type: "ImportDefaultSpecifier"
                • start: 20
                • end: 28
                • loc: {...}
                  • start: {...}
                    • line: 2
                    • column: 11
                    }
                  • end: {...}
                    • line: 2
                    • column: 19
                    }
                  }
                • local: Identifier {...}
                  • type: "Identifier"
                  • start: 20
                  • end: 28
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 11
                      }
                    • end: {...}
                      • line: 2
                      • column: 19
                      }
                    }
                  • name: "setFocus"
                  }
                }
              ]
            • source: Literal {...}
              • type: "Literal"
              • start: 34
              • end: 62
              • loc: {...}
                • start: {...}
                  • line: 2
                  • column: 25
                  }
                • end: {...}
                  • line: 2
                  • column: 53
                  }
                }
              • value: "@svackages/set-focus@1.0.1"
              • raw: "\"@svackages/set-focus@1.0.1\""
              }
            }
          • ImportDeclaration {...}
            • type: "ImportDeclaration"
            • start: 68
            • end: 109
            • loc: {...}
              • start: {...}
                • line: 3
                • column: 4
                }
              • end: {...}
                • line: 3
                • column: 45
                }
              }
            • specifiers: [...] (1)
              • ImportSpecifier {...}
                • type: "ImportSpecifier"
                • start: 77
                • end: 81
                • loc: {...}
                  • start: {...}
                    • line: 3
                    • column: 13
                    }
                  • end: {...}
                    • line: 3
                    • column: 17
                    }
                  }
                • imported: Identifier {...}
                  • type: "Identifier"
                  • start: 77
                  • end: 81
                  • loc: {...}
                    • start: {...}
                      • line: 3
                      • column: 13
                      }
                    • end: {...}
                      • line: 3
                      • column: 17
                      }
                    }
                  • name: "fade"
                  }
                • local: Identifier {...}
                  • type: "Identifier"
                  • start: 77
                  • end: 81
                  • loc: {...}
                    • start: {...}
                      • line: 3
                      • column: 13
                      }
                    • end: {...}
                      • line: 3
                      • column: 17
                      }
                    }
                  • name: "fade"
                  }
                }
              ]
            • source: Literal {...}
              • type: "Literal"
              • start: 89
              • end: 108
              • loc: {...}
                • start: {...}
                  • line: 3
                  • column: 25
                  }
                • end: {...}
                  • line: 3
                  • column: 44
                  }
                }
              • value: "svelte/transition"
              • raw: "\"svelte/transition\""
              }
            }
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 115
            • end: 139
            • loc: {...}
              • start: {...}
                • line: 5
                • column: 4
                }
              • end: {...}
                • line: 5
                • column: 28
                }
              }
            • declarations: [...] (1)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 119
                • end: 138
                • loc: {...}
                  • start: {...}
                    • line: 5
                    • column: 8
                    }
                  • end: {...}
                    • line: 5
                    • column: 27
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 119
                  • end: 130
                  • loc: {...}
                    • start: {...}
                      • line: 5
                      • column: 8
                      }
                    • end: {...}
                      • line: 5
                      • column: 19
                      }
                    }
                  • name: "showOverlay"
                  }
                • init: Literal {...}
                  • type: "Literal"
                  • start: 133
                  • end: 138
                  • loc: {...}
                    • start: {...}
                      • line: 5
                      • column: 22
                      }
                    • end: {...}
                      • line: 5
                      • column: 27
                      }
                    }
                  • value: false
                  • raw: "false"
                  }
                }
              ]
            • kind: "let"
            }
          ]
        • sourceType: "module"
        }
      • attributes: []
      }
    }
The AST is not public API and may change at any point in time
setFocus • Playground • Svelte