snackbar

package
Version: v0.0.0-...-a8d3157 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2018 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

snackbar implements a material snackbar component.

See: https://material.io/components/web/catalog/snackbars/

Example
package main

import (
	"fmt"
	"log"

	"agamigo.io/material/internal/mdctest"
	"agamigo.io/material/snackbar"
	"github.com/gopherjs/gopherjs/js"
)

func main() {
	// Create a new instance of a material snackbar component.
	c := snackbar.New()
	printName(c)
	printState(c)
	c.Message = "snackbar message. before Start()"
	c.ActionHandler = func() { fmt.Println("Action Handled! before Start()") }
	c.ActionText = "Action Button Text. before Start()"
	c.Timeout = 1000 // 1 second
	c.MultiLine = true
	c.ActionOnBottom = true
	c.DismissOnAction = true
	printState(c)

	// Set up a DOM HTMLElement suitable for a snackbar.
	js.Global.Get("document").Get("body").Set("innerHTML",
		mdctest.HTML(c.Component().Type.MDCClassName))
	rootElem := js.Global.Get("document").Get("body").Get("firstElementChild")

	// Start the component, which associates it with an HTMLElement.
	err := c.Start(rootElem)
	if err != nil {
		log.Fatalf("Unable to start component %s: %v\n",
			c.Component().Type, err)
	}

	printState(c)
	c.Message = "snackbar message. after Start()"
	c.ActionHandler = func() { fmt.Println("Action Handled! after Start()") }
	c.ActionText = "Action Button Text. after Start()"
	c.Timeout = 2000 // 2 second
	c.MultiLine = false
	c.ActionOnBottom = false
	c.DismissOnAction = false
	err = c.Show()
	if err != nil {
		log.Fatalf("Unable to show snackbar: %v", err)
	}
	printState(c)

	err = c.Stop()
	if err != nil {
		log.Fatalf("Unable to stop component %s: %v\n",
			c.Component().Type, err)
	}
	printState(c)

}

func printName(c *snackbar.S) {
	fmt.Printf("%s\n", c.Component().Type)
}

func printState(c *snackbar.S) {
	fmt.Printf("DismissOnAction: %v\n",
		c.Component().Get("dismissOnAction"))

	fmt.Printf("[Go] Message: %v, Timeout: %v, ActionHandler Exists: %v,"+
		" ActionText: %v", c.Message, c.Timeout,
		c.Component().Get("actionHandler") != nil, c.ActionText)
	fmt.Printf("Multiline: %v, ActionOnBottom: %v\n",
		c.MultiLine, c.ActionOnBottom)
	if c.Component().Get("foundation_") != js.Undefined {
		o := c.Component().Get("foundation_").Get("snackbarData_")
		if o == nil {
			fmt.Println("Snackbar has not been shown yet.")
			return
		}
		fmt.Printf("[Js] Message: %v, Timeout: %v, ActionHandler: %v, ActionText: %v",
			o.Get("message"),
			o.Get("timeout"),
			o.Get("actionHandler").Interface(),
			o.Get("actionText"),
		)
		fmt.Printf("Multiline: %v, ActionOnBottom: %v\n",
			o.Get("multiline"),
			o.Get("actionOnBottom"),
		)
	}
}

func init() {
	// We emulate a DOM here since tests run in NodeJS.
	// Not needed when running in a browser.
	err := mdctest.Init()
	if err != nil {
		log.Fatalf("Unable to setup test environment: %v", err)
	}
}
Output:

MDCSnackbar
DismissOnAction: false
[Go] Message: , Timeout: 2750, ActionHandler Exists: false, ActionText: Multiline: false, ActionOnBottom: false
DismissOnAction: true
[Go] Message: snackbar message. before Start(), Timeout: 1000, ActionHandler Exists: true, ActionText: Action Button Text. before Start()Multiline: true, ActionOnBottom: true
DismissOnAction: true
[Go] Message: snackbar message. before Start(), Timeout: 1000, ActionHandler Exists: true, ActionText: Action Button Text. before Start()Multiline: false, ActionOnBottom: true
Snackbar has not been shown yet.
DismissOnAction: false
[Go] Message: snackbar message. after Start(), Timeout: 2000, ActionHandler Exists: true, ActionText: Action Button Text. after Start()Multiline: false, ActionOnBottom: false
[Js] Message: snackbar message. after Start(), Timeout: 2000, ActionHandler: 0x1, ActionText: Action Button Text. after Start()Multiline: false, ActionOnBottom: false
DismissOnAction: false
[Go] Message: snackbar message. after Start(), Timeout: 2000, ActionHandler Exists: true, ActionText: Action Button Text. after Start()Multiline: false, ActionOnBottom: false
[Js] Message: snackbar message. after Start(), Timeout: 2000, ActionHandler: 0x1, ActionText: Action Button Text. after Start()Multiline: false, ActionOnBottom: false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type S

type S struct {

	// DismissOnAction causes the snackbar to be dimissed when the user presses
	// the action button. If you want the snackbar to remain visible until the
	// timeout is reached (regardless of whether the user pressed the action
	// button or not) you can set this to to false.
	DismissOnAction bool `js:"dismissOnAction"`

	// The text message to display.
	// Required.
	Message string `js:"message"`

	// The amount of time in milliseconds to show the snackbar.
	// Default is 2750.
	Timeout int `js:"timeout"`

	// The function to execute when the action is clicked.
	// Optional.
	ActionHandler func() `js:"actionHandler"`

	// The text to display for the action button.
	// Required if actionHandler is set.
	ActionText string `js:"actionText"`

	// Whether to show the snackbar with space for multiple lines of text.
	// Default is false.
	MultiLine bool `js:"multiline"`

	// Whether to show the action below the multiple lines of text.
	// Optional, applies when multiline is true. Default is false.
	ActionOnBottom bool `js:"actionOnBottom"`
	// contains filtered or unexported fields
}

S is a material snackbar component.

func New

func New() *S

New returns a new component.

func (*S) Component

func (c *S) Component() *base.Component

Component returns the component's underlying base.Component.

func (*S) Show

func (c *S) Show() error

Show displays the snackbar. If the configuration is invalid an error message will be returned and the snackbar will not be shown. For information on config requirements look at documentation for S.

func (*S) Start

func (c *S) Start(rootElem *js.Object) error

Start initializes the component with an existing HTMLElement, rootElem. Start should only be used on a newly created component, or after calling Stop.

func (*S) StateMap

func (c *S) StateMap() base.StateMap

StateMap implements the base.StateMapper interface.

func (*S) Stop

func (c *S) Stop() error

Stop removes the component's association with its HTMLElement and cleans up event listeners, etc.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL