-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from chasefleming/chasefleming/stylemanager
Enable advanced styling with StyleManager
- Loading branch information
Showing
12 changed files
with
816 additions
and
4 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
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,40 @@ | ||
package elem | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
// MockStyleManager simulates the StyleManager for testing purposes. | ||
type MockStyleManager struct{} | ||
|
||
// GenerateCSS returns a fixed CSS string for testing. | ||
func (m *MockStyleManager) GenerateCSS() string { | ||
return "body { background-color: #fff; }" | ||
} | ||
|
||
func TestRenderWithOptionsInjectsCSSIntoHead(t *testing.T) { | ||
// Setup a simple element that represents an HTML document structure | ||
e := &Element{ | ||
Tag: "html", | ||
Children: []Node{ | ||
&Element{Tag: "head"}, | ||
&Element{Tag: "body"}, | ||
}, | ||
} | ||
|
||
// Use the MockStyleManager | ||
mockStyleManager := &MockStyleManager{} | ||
|
||
// Assuming RenderOptions expects a StyleManager interface, pass the mock | ||
opts := RenderOptions{ | ||
StyleManager: mockStyleManager, // This should be adjusted to how your options are structured | ||
} | ||
htmlOutput := e.RenderWithOptions(opts) | ||
|
||
// Construct the expected HTML string with the CSS injected | ||
expectedHTML := "<!DOCTYPE html><html><head><style>body { background-color: #fff; }</style></head><body></body></html>" | ||
|
||
// Use testify's assert.Equal to check if the HTML output matches the expected HTML | ||
assert.Equal(t, expectedHTML, htmlOutput, "The generated HTML should include the CSS in the <head> section") | ||
} |
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 @@ | ||
# Advanced Styling App with elem-go and StyleManager | ||
|
||
This web application demonstrates the power of advanced CSS styling within a Go server environment, using the `elem-go` library and its `StyleManager` for dynamic styling. It features a button with hover effects, an animated element, and a responsive design element that adjusts styles based on the viewport width. | ||
|
||
## Prerequisites | ||
|
||
Ensure that Go is installed on your system. | ||
|
||
## Installation | ||
|
||
Clone or download the repository, then run the following commands to download the necessary packages: | ||
|
||
```bash | ||
go mod tidy | ||
``` | ||
|
||
This will install `elem-go` and the `styles` subpackage required to run the application. | ||
|
||
## Running the Application | ||
|
||
To run the application, execute the following command: | ||
|
||
```bash | ||
go run main.go | ||
``` | ||
|
||
The server will start on `localhost` at port `8080`. You can view the application by navigating to `http://localhost:8080` in your web browser. | ||
|
||
## Features | ||
|
||
**Button Hover Effect**: The button changes color and scale when hovered over, providing visual feedback to the user. | ||
**Animated Element**: The animated element moves across the screen in a loop, demonstrating the use of animations with `StyleManager`. | ||
**Responsive Design**: The application adjusts the background color based on the viewport width, showcasing the use of media queries for responsive styling. | ||
|
||
## Advanced CSS Styling with `StyleManager` | ||
|
||
The `StyleManager` within the `styles` package provides a powerful solution for managing CSS styles in Go-based web applications. It enables the creation of dynamic and responsive styles, including pseudo-classes, animations, and media queries, directly in Go. | ||
|
||
To learn more about `StyleManager` and its advanced features, refer to the [StyleManager documentation](../../styles/STYLEMANAGER.md). | ||
|
||
## Stopping the Server | ||
|
||
To stop the application, press `Ctrl + C` in the terminal where the server is running. |
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,7 @@ | ||
module stylemanager_demo | ||
|
||
go 1.21.1 | ||
|
||
require github.com/chasefleming/elem-go v0.0.0 | ||
|
||
replace github.com/chasefleming/elem-go => ../../../elem-go |
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,8 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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,80 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/chasefleming/elem-go" | ||
"github.com/chasefleming/elem-go/attrs" | ||
"github.com/chasefleming/elem-go/styles" | ||
"net/http" | ||
) | ||
|
||
func generateWebContent() string { | ||
// Initialize StyleManager | ||
styleMgr := styles.NewStyleManager() | ||
|
||
// Button with hover effect | ||
buttonClass := styleMgr.AddCompositeStyle(styles.CompositeStyle{ | ||
Default: styles.Props{ | ||
styles.Padding: "10px 20px", | ||
styles.BackgroundColor: "blue", | ||
styles.Color: "white", | ||
styles.Border: "none", | ||
styles.Cursor: "pointer", | ||
styles.Margin: "10px", | ||
}, | ||
PseudoClasses: map[string]styles.Props{ | ||
"hover": { | ||
styles.BackgroundColor: "darkblue", | ||
}, | ||
}, | ||
}) | ||
|
||
// Animated element | ||
animationName := styleMgr.AddAnimation(styles.Keyframes{ | ||
"0%": {styles.Transform: "translateY(0px)"}, | ||
"50%": {styles.Transform: "translateY(-20px)"}, | ||
"100%": {styles.Transform: "translateY(0px)"}, | ||
}) | ||
animatedClass := styleMgr.AddStyle(styles.Props{ | ||
styles.Width: "100px", | ||
styles.Height: "100px", | ||
styles.BackgroundColor: "green", | ||
styles.AnimationName: animationName, | ||
styles.AnimationDuration: "2s", | ||
styles.AnimationIterationCount: "infinite", | ||
}) | ||
|
||
// Responsive design | ||
responsiveClass := styleMgr.AddCompositeStyle(styles.CompositeStyle{ | ||
Default: styles.Props{ | ||
styles.Padding: "20px", | ||
styles.BackgroundColor: "lightgray", | ||
styles.Margin: "10px", | ||
}, | ||
MediaQueries: map[string]styles.Props{ | ||
"@media (max-width: 600px)": { | ||
styles.Padding: "10px", | ||
styles.BackgroundColor: "lightblue", | ||
}, | ||
}, | ||
}) | ||
|
||
// Composing the page | ||
pageContent := elem.Div(nil, | ||
elem.Button(attrs.Props{attrs.Class: buttonClass}, elem.Text("Hover Over Me")), | ||
elem.Div(attrs.Props{attrs.Class: animatedClass}, elem.Text("I animate!")), | ||
elem.Div(attrs.Props{attrs.Class: responsiveClass}, elem.Text("Resize the window")), | ||
) | ||
|
||
// Render with StyleManager | ||
return pageContent.RenderWithOptions(elem.RenderOptions{StyleManager: styleMgr}) | ||
} | ||
|
||
func main() { | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
htmlContent := generateWebContent() // Assume this returns the HTML string | ||
w.Header().Set("Content-Type", "text/html") | ||
w.Write([]byte(htmlContent)) | ||
}) | ||
|
||
http.ListenAndServe(":8080", nil) | ||
} |
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
Oops, something went wrong.