-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace the themed with a full example
- Loading branch information
1 parent
c9f1441
commit 671326f
Showing
12 changed files
with
302 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Full example with mutex | ||
|
||
This example hase the following: | ||
|
||
* A `StyleSheet` that generates global resets, base styles and includes themes. | ||
* The `Stylesheet` has a `Mutex` that generates the CSS only once to avoid multiple builds. | ||
* Both `body` and `buttons` have styles attached to the `StyleSheet` as well as the `Theme` to ensure the css is loaded in the most appropriate places. | ||
|
||
## Example output | ||
|
||
```css | ||
*, ::after, ::before, ::backdrop, ::file-selector-button { | ||
border: 0 solid; | ||
box-sizing: border-box; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
|
||
html, :host { | ||
font-family: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; | ||
line-height: 1.5; | ||
-webkit-text-size-adjust: 100%; | ||
tab-size: 4; | ||
} | ||
|
||
body { | ||
line-height: inherit; | ||
} | ||
|
||
body { | ||
min-height: 100vh; | ||
} | ||
|
||
.button { | ||
align-items: center; | ||
border-radius: 0.375rem; | ||
display: inline-flex; | ||
font-size: 0.875rem; | ||
font-weight: 500; | ||
height: 2.500rem; | ||
justify-content: center; | ||
line-height: 1.250rem; | ||
padding-bottom: 0.500rem; | ||
padding-left: 1.000rem; | ||
padding-right: 1.000rem; | ||
padding-top: 0.500rem; | ||
} | ||
|
||
@media (prefers-color-scheme: light) { | ||
body { | ||
background-color: rgba(255, 255, 255, 1.00); | ||
color: rgba(23, 23, 23, 1.00); | ||
} | ||
|
||
.button-primary { | ||
background-color: rgba(23, 23, 23, 1.00); | ||
color: rgba(255, 255, 255, 1.00); | ||
} | ||
} | ||
|
||
@media (prefers-color-scheme: dark) { | ||
body { | ||
background-color: rgba(23, 23, 23, 1.00); | ||
color: rgba(245, 245, 245, 1.00); | ||
} | ||
|
||
.button-primary { | ||
background-color: rgba(255, 255, 255, 1.00); | ||
color: rgba(23, 23, 23, 1.00); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package styles | ||
|
||
import ( | ||
"github.com/AccentDesign/gcss" | ||
"github.com/AccentDesign/gcss/variables" | ||
) | ||
|
||
// Base returns the base styles for the stylesheet. | ||
func (ss *StyleSheet) Base() Styles { | ||
return Styles{ | ||
{ | ||
Selector: "body", | ||
Props: gcss.Props{ | ||
MinHeight: variables.FullScreenHeight, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Base returns the base styles for the theme. | ||
func (t *Theme) Base() Styles { | ||
return Styles{ | ||
{ | ||
Selector: "body", | ||
Props: gcss.Props{ | ||
BackgroundColor: t.Background, | ||
Color: t.Foreground, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package styles | ||
|
||
import ( | ||
"github.com/AccentDesign/gcss" | ||
"github.com/AccentDesign/gcss/props" | ||
"github.com/AccentDesign/gcss/variables" | ||
) | ||
|
||
// Buttons returns the buttons styles for the stylesheet. | ||
func (ss *StyleSheet) Buttons() Styles { | ||
return Styles{ | ||
{ | ||
Selector: ".button", | ||
Props: gcss.Props{ | ||
AlignItems: props.AlignItemsCenter, | ||
BorderRadius: variables.Size1H, | ||
Display: props.DisplayInlineFlex, | ||
FontSize: variables.Size3H, | ||
FontWeight: props.FontWeightMedium, | ||
Height: variables.Size10, | ||
JustifyContent: props.JustifyContentCenter, | ||
LineHeight: variables.Size5, | ||
PaddingTop: variables.Size2, | ||
PaddingRight: variables.Size4, | ||
PaddingBottom: variables.Size2, | ||
PaddingLeft: variables.Size4, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Buttons returns the buttons styles for the theme. | ||
func (t *Theme) Buttons() Styles { | ||
return Styles{ | ||
{ | ||
Selector: ".button-primary", | ||
Props: gcss.Props{ | ||
BackgroundColor: t.Primary, | ||
Color: t.PrimaryForeground, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package styles | ||
|
||
import ( | ||
"github.com/AccentDesign/gcss" | ||
"github.com/AccentDesign/gcss/props" | ||
"github.com/AccentDesign/gcss/variables" | ||
) | ||
|
||
// Resets returns the resets styles for the stylesheet. | ||
func (ss *StyleSheet) Resets() Styles { | ||
return Styles{ | ||
{ | ||
Selector: "*,::after,::before,::backdrop,::file-selector-button", | ||
Props: gcss.Props{ | ||
Border: props.Border{ | ||
Width: variables.Size0, | ||
Style: props.BorderStyleSolid, | ||
}, | ||
BoxSizing: props.BoxSizingBorderBox, | ||
Margin: variables.Size0, | ||
Padding: variables.Size0, | ||
}, | ||
}, | ||
{ | ||
Selector: "html,:host", | ||
Props: gcss.Props{ | ||
FontFamily: props.FontFamilySans, | ||
LineHeight: props.UnitRaw(1.5), | ||
}, | ||
CustomProps: []gcss.CustomProp{ | ||
{Attr: "-webkit-text-size-adjust", Value: "100%"}, | ||
{Attr: "tab-size", Value: "4"}, | ||
}, | ||
}, | ||
{ | ||
Selector: "body", | ||
Props: gcss.Props{ | ||
LineHeight: props.UnitInherit(), | ||
}, | ||
}, | ||
// more resets | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package styles | ||
|
||
import ( | ||
"bytes" | ||
"github.com/AccentDesign/gcss" | ||
"github.com/AccentDesign/gcss/variables" | ||
"io" | ||
"slices" | ||
"sync" | ||
) | ||
|
||
type ( | ||
Styles []gcss.Style | ||
StyleSheet struct { | ||
Themes []*Theme | ||
css bytes.Buffer | ||
mutex sync.Mutex | ||
} | ||
) | ||
|
||
// CSS writes the stylesheet to the writer. | ||
// It will generate the CSS for the stylesheet if it has not been generated yet. | ||
func (ss *StyleSheet) CSS(w io.Writer) error { | ||
ss.mutex.Lock() | ||
defer ss.mutex.Unlock() | ||
|
||
if ss.css.Len() > 0 { | ||
_, err := w.Write(ss.css.Bytes()) | ||
return err | ||
} | ||
|
||
// Generate the CSS for the global styles. | ||
for _, style := range slices.Concat( | ||
ss.Resets(), | ||
ss.Base(), | ||
ss.Buttons(), | ||
) { | ||
if err := style.CSS(&ss.css); err != nil { | ||
return err | ||
} | ||
} | ||
// Generate the CSS for the themes. | ||
for _, theme := range ss.Themes { | ||
if err := theme.CSS(&ss.css); err != nil { | ||
return err | ||
} | ||
} | ||
// Write the CSS to the writer. | ||
_, err := w.Write(ss.css.Bytes()) | ||
return err | ||
} | ||
|
||
// NewStyleSheet returns a new stylesheet. | ||
func NewStyleSheet() *StyleSheet { | ||
return &StyleSheet{ | ||
Themes: []*Theme{ | ||
{ | ||
MediaQuery: "@media (prefers-color-scheme: light)", | ||
Background: variables.White, | ||
Foreground: variables.Neutral900, | ||
Primary: variables.Neutral900, | ||
PrimaryForeground: variables.White, | ||
}, | ||
{ | ||
MediaQuery: "@media (prefers-color-scheme: dark)", | ||
Background: variables.Neutral900, | ||
Foreground: variables.Neutral100, | ||
Primary: variables.White, | ||
PrimaryForeground: variables.Neutral900, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package styles | ||
|
||
import ( | ||
"fmt" | ||
"github.com/AccentDesign/gcss/props" | ||
"io" | ||
"slices" | ||
) | ||
|
||
type Theme struct { | ||
MediaQuery string | ||
Background props.Color | ||
Foreground props.Color | ||
Primary props.Color | ||
PrimaryForeground props.Color | ||
} | ||
|
||
func (t *Theme) CSS(w io.Writer) error { | ||
if _, err := fmt.Fprintf(w, "%s{", t.MediaQuery); err != nil { | ||
return err | ||
} | ||
for _, style := range slices.Concat( | ||
t.Base(), | ||
t.Buttons(), | ||
) { | ||
if err := style.CSS(w); err != nil { | ||
return err | ||
} | ||
} | ||
if _, err := fmt.Fprint(w, "}"); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.