Setting a Widget’s Properties

A widget’s properties are exposed from a custom element automatically as either attributes, events, or properties.

The following EmailSignup widget contains several different property types.

src/widgets/EmailSignup.tsx

  1. import { tsx, create } from '@dojo/framework/core/vdom';
  2. import * as css from './styles/EmailSignup.m.css';
  3. import icache from '@dojo/framework/core/middleware/icache';
  4. export interface EmailSignupProperties {
  5. title?: string;
  6. onSignUp?: () => void;
  7. signUpBonusDate?: Date;
  8. successMessages?: string[];
  9. }
  10. function randomEntry(arr: string[]) {
  11. return arr[Math.floor(Math.random() * arr.length)];
  12. }
  13. const factory = create({ icache }).properties<EmailSignupProperties>();
  14. export default factory(function EmailSignup({ middleware: { icache }, properties }) {
  15. const { title, signUpBonusDate, successMessages } = properties();
  16. const signedUp = icache.getOrSet('signedUp', false);
  17. const signUp = () => {
  18. const { onSignUp } = properties();
  19. // post email address to a server
  20. icache.set('signedUp', true);
  21. icache.set('successMessage', successMessages ? randomEntry(successMessages) : 'Thank you for signing up!');
  22. onSignUp && onSignUp();
  23. };
  24. return (
  25. <div key="root" classes={css.root}>
  26. <div key="title" classes={css.title}>
  27. {title || 'Subscribe Now'}
  28. </div>
  29. {signedUp ? (
  30. <div key="confirmation" classes={css.thanks}>
  31. {icache.get('successMessage')}
  32. </div>
  33. ) : (
  34. <div key="signup" classes={css.signup}>
  35. {signUpBonusDate && <div>Sign up by {signUpBonusDate.toLocaleDateString()}</div>}
  36. <input type="text" />
  37. <button onclick={signUp}>Sign Up</button>
  38. </div>
  39. )}
  40. </div>
  41. );
  42. });

Attributes

string and object typed widget properties are automatically added as HTML attributes to your custom element. Continuing with the email subscription example, the title property ({ title?: string }) can be customized by adding a title attribute to the HTML. Since title is a string property, the value of the HTML attribute is used as the value of the title property with no transformation.

  1. <ref-email-signup id="emailSignup" title="Subscribe to Our Newsletter"></ref-email-signup>

Other serializable properties, like arrays and configuration objects, can be set via HTML attributes by passing in serialized JSON. The sucessMessages property ({ successMessages?: string[] }) can be set by adding a successmessages attribute to the HTML.

  1. <ref-email-signup
  2. id="emailSignup"
  3. title="Subscribe to Our Newsletter"
  4. successmessages='["Thanks!","Thank you for signing up","You are the best!"]'
  5. ></ref-email-signup>

Here, the value of the successmessages attribute will be deserialized and set as the successMessages property.

Note that if the attribute’s value is not valid JSON, the property will not be set.

Events

Widget properties that start with on and have a function signature are exposed as events. The emitted event is the lower cased part of the property name without the “on.” Listening for the onSignUp property ({ onSignUp?: () => void }) would look like this.

  1. <script>
  2. emailSignup.addEventListener('signup', () => {
  3. // start confetti animation 🎉
  4. });
  5. </script>

Properties

All non-event widget properties are automatically created as properties on the custom element. These properties can be fully controlled by JavaScript. In the email subscription widget, both the title and signUpBonusDate properties can be set programmatically as properties.

  1. <script>
  2. const now = new Date();
  3. emailSignup.signUpBonusDate = new Date(
  4. now.getFullYear(),
  5. now.getMonth(),
  6. now.getDate() + 1
  7. );
  8. </script>