Skip to content

Commit

Permalink
shippin it
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-haskell committed Apr 27, 2021
1 parent 8553d14 commit 3d6830f
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 16 deletions.
14 changes: 12 additions & 2 deletions docs/public/content/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,24 @@ Explore the elm-spa's user authentication API.

[![Example 4 screenshot](/content/images/04-authentication.png)](/examples/04-authentication)

## Realworld example
## Real world examples

Implements the [Realworld app project](), inspired by Richard Feldman's "elm-spa-example" project.
### RealWorld Conduit App

Implements the [RealWorld app](https://github.com/gothinkster/realworld), inspired by Richard Feldman's "elm-spa-example" project.

[![Realworld app screenshot](/content/images/realworld.png)](https://realworld.elm-spa.dev)

Source code: [GitHub](https://github.com/ryannhg/elm-spa-realworld)

### This website

The website you are looking at _right now_ was built with __elm-spa__. Mindbending, right?

[![Realworld app screenshot](/content/images/this-site.png)](https://elm-spa.dev)

Source code: [GitHub](https://github.com/ryannhg/elm-spa/tree/main/docs)

## More examples

There are more examples available on the official repo:
Expand Down
5 changes: 1 addition & 4 deletions docs/public/content/examples/01-hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ npm install -g elm-spa@latest

This will allow you to create a new project using the following commands:

```
mkdir 01-hello-world
cd 01-hello-world
```terminal
elm-spa new
```

Expand Down
96 changes: 91 additions & 5 deletions docs/public/content/examples/02-pages.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Pages & routing

__Source code__: [GitHub](https://github.com/ryannhg/elm-spa/tree/main/examples/01-pages)
__Source code__: [GitHub](https://github.com/ryannhg/elm-spa/tree/main/examples/02-pages)

This next guide will show you how pages, routing, and the `elm-spa add` command work together to automatically handle URLs in your __elm-spa__ application.

Expand All @@ -10,15 +10,15 @@ This next guide will show you how pages, routing, and the `elm-spa add` command
Just like with the last guide, we can use `elm-spa new` and `elm-spa server` to get a brand new __elm-spa__ project up and running:

```terminal
mkdir 02-pages
cd 02-pages
elm-spa new
elm-spa server
```

This generates the "Hello, world!" homepage from before:

```terminal
elm-spa server
```

![A browser displaying "Hello world"](/content/images/01-hello-world.png)

### Adding a static page
Expand Down Expand Up @@ -203,6 +203,92 @@ After creating `style.css`, we can import the file in our `public/index.html` en

Using the `<link>` tag as shown above (with the leading slash!) imports our CSS file. All files in the `public` folder are available at the root of our web application. That means a file stored at `public/images/dog.png` would be at `http://localhost:1234/images/dog`, without including `public` in the URL at all.

### Adding more page types

```terminal
elm-spa add /sandbox sandbox
elm-spa add /element element
elm-spa add /advanced advanced
```

These commands add in the other three page types described in the [pages guide](/guides/03-pages).

For each page, the `View.placeholder` function stubs out the `view` functions so you can visit them in the browser.

For example, [http://localhost:1234/element](http://localhost:1234/element) should display "Element" on the screen.

### Adding some dynamic routes

To add in dynamic routes, we can use `elm-spa add` again:

```terminal
elm-spa add /dynamic/:name static
```

With this command, we just created a page at `src/Pages/Dynamic/Name_.elm`. When a user visits a URL like `/dynamic/ryan` or `dynamic/123`, we'll be taken to this page.

Let's tweak the default `view` function to render the dynamic `name` parameter from the URL:

```elm
-- src/Pages/Dynamic/Name_.elm

view : Params -> View msg
view params =
{ title = "Dynamic: " ++ params.name
, body =
UI.layout
[ UI.h1 "Dynamic Page"
, Html.h2 [] [ Html.text params.name ]
]
}
```

We can provide in the `req.params` to the `view` function by telling our `page` function to pass it along:

```elm
page : Shared.Model -> Request.With Params -> Page
page _ req =
Page.static -- 👇 we pass in params here
{ view = view req.params
}
```

### Updating the navbar

Once we wire up these pages to use `UI.layout`, we can add links to the navbar:

```elm
-- src/UI.elm
import Gen.Route as Route

layout : List (Html msg) -> List (Html msg)
layout children =
let
viewLink : String -> Route -> Html msg
viewLink label route =
Html.a [ Attr.href (Route.toHref route) ] [ Html.text label ]
in
[ Html.div [ Attr.class "container" ]
[ Html.header [ Attr.class "navbar" ]
[ Html.strong [ Attr.class "brand" ] [ viewLink "Home" Route.Home_ ]
, viewLink "Static" Route.Static
, viewLink "Sandbox" Route.Sandbox
, viewLink "Element" Route.Element
, viewLink "Advanced" Route.Advanced
, Html.div [ Attr.class "splitter" ] []
, viewLink "Dynamic: Apple" (Route.Dynamic__Name_ { name = "apple" })
, viewLink "Dynamic: Banana" (Route.Dynamic__Name_ { name = "banana" })
]
, Html.main_ [] children
]
]
```

#### That's it!

Feel free to play around with the `elm-spa add` command to mix-and-match different pages.

As always, the source code for this example is available on [GitHub](https://github.com/ryannhg/elm-spa/tree/main/examples/02-pages)

---

Expand Down
6 changes: 1 addition & 5 deletions docs/public/content/examples/04-authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,12 @@ Feel free to follow along by creating a new __elm-spa__ project:
npm install -g elm-spa@latest
```

```
mkdir user-auth-demo
cd user-auth-demo
```terminal
elm-spa new
```

This will create a new project that you can run with the `elm-spa server` command!

The complete working example is also available at [examples/03-user-auth](https://github.com/ryannhg/elm-spa/tree/master/examples/03-user-auth) on GitHub.

### Ejecting Auth.elm

There's a default file that has this code stubbed out for you in the `.elm-spa/defaults` folder. Let's eject that file into our `src` folder so we can edit it:
Expand Down
Binary file added docs/public/content/images/this-site.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions docs/public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,15 @@ hr { border: 0; }
text-shadow: 0 0 0.5em rgb(0 0 0 / 25%);
}

.home__section pre {
font-size: 1.1em;
max-width: 16em;
padding: 0.85rem 1rem;
}
.home__section code {
font-size: 1.2em;
}

.home__section > .col {
position: relative;
z-index: 1;
Expand Down

0 comments on commit 3d6830f

Please sign in to comment.