go-app icon indicating copy to clipboard operation
go-app copied to clipboard

Bootstrap for styling

Open wraza2k9 opened this issue 2 years ago • 7 comments

How can we use bootstrap themes for styling the components?

wraza2k9 avatar Apr 06 '22 17:04 wraza2k9

Like including their stater components (see #712) and then just using them. What is it that holds you back?

Maybe check out https://github.com/mlctrez/goapp-mdc/ for inspiration.

oderwat avatar Apr 06 '22 18:04 oderwat

There is also the subpackage ui in go-app that provide some stuff.

maxence-charriere avatar Apr 07 '22 01:04 maxence-charriere

I have a problem with bootstrap. The basic components work, but treeview doesn't work. Please help with advice.

package main

import (
	"log"
	"net/http"

	"github.com/maxence-charriere/go-app/v9/pkg/app"
)

type hello struct {
	app.Compo
}

func (h *hello) Render() app.UI {
	return app.Div().Body(
		app.H1().Text("Hello World!"),
		app.Div().ID("tree"),

		app.Script().Src("https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"),
		app.Script().Src("https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"),
		app.Script().Src("https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bstreeview.min.js"),
		app.Raw(`
			<script>
			function getTree() {
				console.log('call getTree')
				var data = [
  					{
    					text: "Node 1",
    					icon: "fa fa-inbox"
					},
  					{
    					text: "Node 2",
    					icon: "fa fa-calendar"
					}
				];
				console.log(data)
				return data;
			  }
			  
			  $('#tree').bstreeview({ data: getTree() });
			</script>
		`),
	)
}

func main() {
	app.Route("/", &hello{})
	app.RunWhenOnBrowser()

	http.Handle("/", &app.Handler{
		Name:        "Hello",
		Description: "An Hello World! example",
		Styles: []string{
			"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css",
			"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css",
			"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bstreeview.min.css",
		},
	})

	if err := http.ListenAndServe(":8000", nil); err != nil {
		log.Fatal(err)
	}
}

maxim0r avatar Apr 15 '22 10:04 maxim0r

@maxim0r

~~It is probably this bit of code in go-app that is causing you not to see dom elements added by your bootstrap library.~~

https://github.com/maxence-charriere/go-app/blob/0bbefa64ec4bdafd261cadc1d0c4e1e97f753f8b/pkg/app/app.go#L62

~~This is by design as go-app needs a known state when it adds and removes elements.~~

Edit : Ok I take that back. I can't see anywhere in the code where that method is called.

mlctrez avatar Apr 15 '22 20:04 mlctrez

Just use app.Script() instead of app.Raw():

func (h *hello) Render() app.UI {
	return app.Div().Body(
		app.H1().Text("Hello World!"),
		app.Div().ID("tree"),

		app.Script().Src("https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"),
		app.Script().Src("https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"),
		app.Script().Src("https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bstreeview.min.js"),
		app.Script().Text(`
			function getTree() {
				console.log('call getTree')
				var data = [
  					{
    					text: "Node 1",
    					icon: "fa fa-inbox"
					},
  					{
    					text: "Node 2",
    					icon: "fa fa-calendar"
					}
				];
				console.log(data)
				return data;
			  }
			  
			  $('#tree').bstreeview({ data: getTree() });
		`),
	)
}

oderwat avatar Apr 15 '22 21:04 oderwat

To explain what happens, app.Raw("<script>window.alert('noBoom');</script>") is basically the same as someNode.innerHTML="<script>window.alert('noBoom');</script>". This is not executed as a security measure.

While app.Script().Text("windows.alert("thaBoom");') creates a script element, sets the content, and then adds the element to the DOM. In the process, the javascript is executed.

oderwat avatar Apr 15 '22 21:04 oderwat

Thank you so much for your help and clarification!

maxim0r avatar Apr 16 '22 06:04 maxim0r