Skip to content

Commit 98c4466

Browse files
schjana-h
andauthored
feat(storybook): add multiple story support for components (#971)
Co-authored-by: Adrian Hesketh <adrianhesketh@hushmail.com> Co-authored-by: Adrian Hesketh <a-h@users.noreply.github.com>
1 parent 3eaa56a commit 98c4466

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

storybook/_example/storybook.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import (
66

77
func Storybook() *storybook.Storybook {
88
s := storybook.New()
9-
s.AddComponent("headerTemplate", headerTemplate, storybook.TextArg("name", "Page Name"))
9+
10+
header := s.AddComponent("headerTemplate", headerTemplate, storybook.TextArg("name", "Page Name"))
11+
header.AddStory("Long Name", storybook.TextArg("name", "A Very Long Page Name"))
12+
1013
s.AddComponent("footerTemplate", footerTemplate)
14+
1115
return s
1216
}

storybook/storybook.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ type Storybook struct {
3636
// Handlers for each of the components.
3737
Handlers map[string]http.Handler
3838
// Handler used to serve Storybook, defaults to filesystem at ./storybook-server/storybook-static.
39-
StaticHandler http.Handler
40-
Header string
41-
Server http.Server
42-
Log *zap.Logger
39+
StaticHandler http.Handler
40+
Header string
41+
Server http.Server
42+
Log *zap.Logger
43+
AdditionalPrefixJS string
4344
}
4445

4546
type StorybookConfig func(*Storybook)
@@ -56,6 +57,20 @@ func WithHeader(header string) StorybookConfig {
5657
}
5758
}
5859

60+
func WithPath(path string) StorybookConfig {
61+
return func(sb *Storybook) {
62+
sb.Path = path
63+
}
64+
}
65+
66+
// WithAdditionalPreviewJS / WithAdditionalPreviewJS allows to add content to the generated .storybook/preview.js file.
67+
// For example this can be used to include custom CSS.
68+
func WithAdditionalPreviewJS(content string) StorybookConfig {
69+
return func(sb *Storybook) {
70+
sb.AdditionalPrefixJS = content
71+
}
72+
}
73+
5974
func New(conf ...StorybookConfig) *Storybook {
6075
cfg := zap.NewProductionConfig()
6176
cfg.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder
@@ -69,23 +84,27 @@ func New(conf ...StorybookConfig) *Storybook {
6984
Handlers: map[string]http.Handler{},
7085
Log: logger,
7186
}
72-
sh.StaticHandler = http.FileServer(http.Dir(path.Join(sh.Path, "storybook-static")))
7387
sh.Server = http.Server{
7488
Handler: sh,
7589
Addr: ":60606",
7690
}
7791
for _, sc := range conf {
7892
sc(sh)
7993
}
94+
95+
// Depends on the correct Path, so must be set after additional config
96+
sh.StaticHandler = http.FileServer(http.Dir(path.Join(sh.Path, "storybook-static")))
97+
8098
return sh
8199
}
82100

83-
func (sh *Storybook) AddComponent(name string, componentConstructor interface{}, args ...Arg) {
101+
func (sh *Storybook) AddComponent(name string, componentConstructor interface{}, args ...Arg) *Conf {
84102
//TODO: Check that the component constructor is a function that returns a templ.Component.
85103
c := NewConf(name, args...)
86104
sh.Config[name] = c
87105
h := NewHandler(name, componentConstructor, args...)
88106
sh.Handlers[name] = h
107+
return c
89108
}
90109

91110
func (sh *Storybook) Build(ctx context.Context) (err error) {
@@ -256,7 +275,7 @@ func (sh *Storybook) configureStorybook() (configHasChanged bool, err error) {
256275
}
257276
configHasChanged = before != after
258277
// Configure storybook Preview URL.
259-
err = os.WriteFile(filepath.Join(sh.Path, ".storybook/preview.js"), []byte(previewJS), os.ModePerm)
278+
err = os.WriteFile(filepath.Join(sh.Path, ".storybook/preview.js"), []byte(fmt.Sprintf("%s\n%s", sh.AdditionalPrefixJS, previewJS)), os.ModePerm)
260279
if err != nil {
261280
return
262281
}
@@ -363,7 +382,7 @@ func (c *Conf) AddStory(name string, args ...Arg) {
363382
}
364383
c.Stories = append(c.Stories, Story{
365384
Name: name,
366-
Args: NewSortedMap(),
385+
Args: m,
367386
})
368387
}
369388

@@ -521,6 +540,6 @@ func (sm *SortedMap) MarshalJSON() (output []byte, err error) {
521540
}
522541

523542
type Story struct {
524-
Name string `json:"name"`
525-
Args *SortedMap
543+
Name string `json:"name"`
544+
Args *SortedMap `json:"args"`
526545
}

0 commit comments

Comments
 (0)