Skip to content

Commit

Permalink
docs: hyperlink buttons demo (#3245)
Browse files Browse the repository at this point in the history
<!-- please provide a detailed description of the changes made in this
pull request. -->

## Description

Adds a `r/docs` that showcases how "buttons" can be added to realm
renders.

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [x] Provided any useful hints for running manual tests
</details>

---------

Co-authored-by: Morgan <[email protected]>
  • Loading branch information
leohhhn and thehowl authored Dec 7, 2024
1 parent 3de2475 commit 7278687
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
44 changes: 44 additions & 0 deletions examples/gno.land/r/docs/buttons/buttons.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package buttons

import (
"std"

"gno.land/p/demo/ufmt"
"gno.land/p/moul/txlink"
)

var (
motd = "The Initial Message\n\n"
lastCaller std.Address
)

func UpdateMOTD(newmotd string) {
motd = newmotd
lastCaller = std.PrevRealm().Addr()
}

func Render(path string) string {
if path == "motd" {
out := "# Message of the Day:\n\n"
out += "---\n\n"
out += "# " + motd + "\n\n"
out += "---\n\n"
link := txlink.Call("UpdateMOTD", "newmotd", "Message!") // "/r/docs/buttons$help&func=UpdateMOTD&newmotd=Message!"
out += ufmt.Sprintf("Click **[here](%s)** to update the Message of The Day!\n\n", link)
out += "[Go back to home page](/r/docs/buttons)\n\n"
out += "Last updated by " + lastCaller.String()

return out
}

out := `# Buttons
Users can create simple hyperlink buttons to view specific realm pages and
do specific realm actions, such as calling a specific function with some arguments.
The foundation for this functionality are markdown links; for example, you can
click...
` + "\n## [here](/r/docs/buttons:motd)\n" + `...to view this realm's message of the day.`

return out
}
14 changes: 14 additions & 0 deletions examples/gno.land/r/docs/buttons/buttons_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package buttons

import (
"strings"
"testing"
)

func TestRenderMotdLink(t *testing.T) {
res := Render("motd")
const wantLink = "/r/docs/buttons$help&func=UpdateMOTD&newmotd=Message!"
if !strings.Contains(res, wantLink) {
t.Fatalf("%s\ndoes not contain correct help page link: %s", res, wantLink)
}
}
1 change: 1 addition & 0 deletions examples/gno.land/r/docs/buttons/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/docs/buttons
1 change: 1 addition & 0 deletions examples/gno.land/r/docs/docs.gno
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Explore various examples to learn more about Gno functionality and usage.
- [Hello World](/r/docs/hello) - A simple introductory example.
- [Adder](/r/docs/adder) - An interactive example to update a number with transactions.
- [Source](/r/docs/source) - View realm source code.
- [Buttons](/r/docs/buttons) - Add buttons to your realm's render.
- [AVL Pager](/r/docs/avl_pager) - Paginate through AVL tree items.
- [Img Embed](/r/docs/img_embed) - Demonstrates how to embed an image.
- ...
Expand Down

0 comments on commit 7278687

Please sign in to comment.