@@ -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
4546type 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+
5974func 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
91110func (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
523542type Story struct {
524- Name string `json:"name"`
525- Args * SortedMap
543+ Name string `json:"name"`
544+ Args * SortedMap `json:"args"`
526545}
0 commit comments