-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b92089
commit a578dcf
Showing
25 changed files
with
685 additions
and
308 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,3 @@ | ||
server: ruby -run -e httpd docs/dist | ||
docs: bundle exec docs/build.rb --watch | ||
tailwind: npx tailwindcss -i ./docs/assets/application.css -o ./docs/dist/application.css --watch |
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 |
---|---|---|
@@ -1,18 +1 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require "listen" | ||
|
||
system "bundle exec docs/build.rb" | ||
|
||
Listen.to("#{__dir__}/../docs/pages", "#{__dir__}/../docs/components") do |modified, added, removed| | ||
puts modified | ||
puts added | ||
puts removed | ||
|
||
system "bundle exec docs/build.rb" | ||
end.start | ||
|
||
system "ruby -run -e httpd docs/dist" | ||
|
||
Process.waitall | ||
foreman start -f Procfile.dev |
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 |
---|---|---|
@@ -1,22 +1,27 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
$stdout.sync = true | ||
|
||
require "phlex" | ||
require "bundler" | ||
require "fileutils" | ||
|
||
Bundler.require :docs | ||
|
||
Zeitwerk::Loader.new.tap do |loader| | ||
loader.push_dir(__dir__) | ||
loader.ignore(__FILE__) | ||
loader.setup | ||
loader.eager_load | ||
end | ||
|
||
FileUtils.mkdir_p("#{__dir__}/dist") | ||
FileUtils.cp_r("#{__dir__}/assets", "#{__dir__}/dist") | ||
loader = Zeitwerk::Loader.new | ||
loader.push_dir(__dir__) | ||
loader.ignore(__FILE__) | ||
loader.enable_reloading | ||
loader.setup | ||
loader.eager_load | ||
|
||
PageBuilder.build_all | ||
|
||
system "npx tailwindcss -i ./docs/assets/application.css -o ./docs/dist/application.css" | ||
if ARGV.include? "--watch" | ||
Filewatcher.new("#{__dir__}/**/*rb").watch do |_changes| | ||
loader.reload | ||
loader.eager_load | ||
PageBuilder.build_all | ||
end | ||
end |
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Components | ||
class CodeSpan < Phlex::View | ||
def template(&block) | ||
code(class: "bg-stone-50 inline-block font-medium rounded border px-1 -mt-1", &block) | ||
end | ||
end | ||
end |
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
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,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module Components | ||
class Nav < Phlex::View | ||
end | ||
end |
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Components | ||
class Nav::Item < Phlex::View | ||
def initialize(text, to:, active_page:) | ||
@text = text | ||
@to = to | ||
@active_page = active_page | ||
end | ||
|
||
def template | ||
li do | ||
a(**link_classes, href: "/#{link}") { @text } | ||
end | ||
end | ||
|
||
def link_classes | ||
classes("pb-1 block font-medium text-stone-500", active?: "text-red-600 font-bold") | ||
end | ||
|
||
def link | ||
path == "index" ? "" : path | ||
end | ||
|
||
def path | ||
@to.name.split("::")[1..].map { _1.gsub(/(.)([A-Z])/, '\1-\2') }.map(&:downcase).join("/") | ||
end | ||
|
||
def active? | ||
@active_page.instance_of?(@to) | ||
end | ||
end | ||
end |
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,97 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pages | ||
class Helpers < ApplicationPage | ||
def template | ||
render Layout.new(title: "Templates in Phlex") do | ||
render Markdown.new(<<~MD) | ||
# Helpers | ||
## Conditional tokens and classes | ||
The `tokens` method helps you define conditional HTML attribute tokens (such as CSS classes). It accepts a splat of tokens that should always be output as well as optional keyword arguments for conditional tokens. | ||
The keyword arguments allow you to specify under which conditions certain tokens are applicable. The keys are the conditions and the values are the tokens. Conditions can be Procs which are evaluated, or Symbols that map to an instance method. The `:active?` Symbol, for example, maps to the `active?` instance method. | ||
Here we have a `Link` view that produces an `<a>` tag with the CSS class `nav-item`. If the link is _active_, we also apply the CSS class `nav-item-active`. | ||
MD | ||
|
||
render Example.new do |e| | ||
e.tab "link.rb", <<~RUBY | ||
class Link < Phlex::View | ||
def initialize(text, to:, active:) | ||
@text = text | ||
@to = to | ||
@active = active | ||
end | ||
def template | ||
a(href: @to, class: tokens("nav-item", | ||
active?: "nav-item-active")) { @text } | ||
end | ||
private | ||
def active? = @active | ||
end | ||
RUBY | ||
|
||
e.tab "example.rb", <<~RUBY | ||
class Example < Phlex::View | ||
def template | ||
nav do | ||
ul do | ||
li { render Link.new("Home", to: "/", active: true) } | ||
li { render Link.new("About", to: "/about", active: false) } | ||
end | ||
end | ||
end | ||
end | ||
RUBY | ||
|
||
e.execute "Example.new.call" | ||
end | ||
|
||
render Markdown.new(<<~MD) | ||
You can also use the `classes` helper method to create a token list of classes. Since this method returns a hash, e.g. `{ class: "your CSS classes here" }`, you can destructure it into a `class:` keyword argument using the `**` prefix operator. | ||
MD | ||
|
||
render Example.new do |e| | ||
e.tab "link.rb", <<~RUBY | ||
class Link < Phlex::View | ||
def initialize(text, to:, active:) | ||
@text = text | ||
@to = to | ||
@active = active | ||
end | ||
def template | ||
a(href: @to, **classes("nav-item", | ||
active?: "nav-item-active")) { @text } | ||
end | ||
private | ||
def active? = @active | ||
end | ||
RUBY | ||
|
||
e.tab "example.rb", <<~RUBY | ||
class Example < Phlex::View | ||
def template | ||
nav do | ||
ul do | ||
li { render Link.new("Home", to: "/", active: true) } | ||
li { render Link.new("About", to: "/about", active: false) } | ||
end | ||
end | ||
end | ||
end | ||
RUBY | ||
|
||
e.execute "Example.new.call" | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.