Skip to content

Instantly share code, notes, and snippets.

@sethbergman
Forked from Vheissu/app.html
Last active July 19, 2017 06:54
Show Gist options
  • Save sethbergman/0ade73e206476152944aea3f0b1b5e44 to your computer and use it in GitHub Desktop.
Save sethbergman/0ade73e206476152944aea3f0b1b5e44 to your computer and use it in GitHub Desktop.
Checking for user provided slots

Here's an example: https://gist.run?id=0ade73e206476152944aea3f0b1b5e44

app.html

<template>
  <require from="./modal"></require>
  
  <modal>
    <div slot="modalHeader">This is user provided header content.</div>
  </modal>
</template>

app.js

export class App {  

}

index.html

<!doctype html>
<html>
  <head>
    <title>Aurelia</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body aurelia-app>
    <h1>Loading...</h1>

    <script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
    <script>
      require(['aurelia-bootstrapper']);
    </script> 
  </body>
</html>

modal.html

<template>
    <div class="modal__header" show.bind="$slots.modalHeader.children.length">
      <h1>Header:</h1>
      <slot name="modalHeader"></slot>
    </div>
    
    <div class="modal__body">
      <h1>Body:</h1>
      <slot></slot>
    </div>
    
    <div class="modal__footer" show.bind="$slots.modalFooter.children.length">
      <h1>Footer:</h1>
      <slot name="modalFooter"></slot>
    </div>
</template>

modal.js

export class ModalCustomElement {
  static inject = [Element];
  
  constructor(element) {
    this.element = element;
  }
  
  attached() {
    this.$slots = this.element.au.controller.view.slots;
    console.log(this.$slots);
  }
}
<template>
<require from="./modal"></require>
<modal>
<div slot="modalHeader">This is user provided header content.</div>
</modal>
</template>
export class App {
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
<script>
require(['aurelia-bootstrapper']);
</script>
</body>
</html>
<template>
<div class="modal__header" show.bind="$slots.modalHeader.children.length">
<h1>Header:</h1>
<slot name="modalHeader"></slot>
</div>
<div class="modal__body">
<h1>Body:</h1>
<slot></slot>
</div>
<div class="modal__footer" show.bind="$slots.modalFooter.children.length">
<h1>Footer:</h1>
<slot name="modalFooter"></slot>
</div>
</template>
export class ModalCustomElement {
static inject = [Element];
constructor(element) {
this.element = element;
}
attached() {
this.$slots = this.element.au.controller.view.slots;
console.log(this.$slots);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment