Skip to content

Commit c290b11

Browse files
committed
add basic How it Works docs
1 parent fd6f814 commit c290b11

9 files changed

Lines changed: 54 additions & 8 deletions

File tree

docs/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Changelog
4-
nav_order: 5
4+
nav_order: 6
55
---
66

77
<!-- Add unreleased changes under the "main" heading. -->

docs/CODE_OF_CONDUCT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Code of conduct
4-
nav_order: 6
4+
nav_order: 7
55
---
66

77
# Contributor Covenant Code of Conduct

docs/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Contributing
4-
nav_order: 7
4+
nav_order: 9
55
---
66

77
# Contributing

docs/compatibility.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Compatibility
4-
nav_order: 6
4+
nav_order: 8
55
---
66

77
# Compatibility

docs/history.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: History
4-
nav_order: 8
4+
nav_order: 10
55
---
66

77
# History

docs/how-it-works.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
layout: default
3+
title: How it works
4+
nav_order: 5
5+
---
6+
7+
## How ViewComponent Works
8+
9+
### At application boot
10+
11+
When `ViewComponent::Base` is inherited from, https://github.com/ViewComponent/view_component/blob/7538b59b4bdc0f561232384adc1deb545071e9cc/lib/view_component/base.rb (line 520), `ViewComponent::Compiler.compile` is called, which compiles a component’s template(s) into instance method(s) on the component:
12+
13+
```ruby
14+
class MyComponent < ViewComponent::Base
15+
end
16+
```
17+
18+
```erb
19+
<%= Time.now %>
20+
```
21+
22+
Compiles to:
23+
24+
```ruby
25+
class MyComponent < ViewComponent::Base
26+
def _call_my_component
27+
@output_buffer.append=( Time.now )
28+
@output_buffer
29+
end
30+
end
31+
```
32+
33+
### At component instantiation
34+
35+
ViewComponent does not perform any actions at component instantiation.
36+
37+
### At render
38+
39+
When a ViewComponent is passed to `render`, Rails calls `render_in` (link), passing the current [ActionView::Context](https://api.rubyonrails.org/classes/ActionView/Context.html). `ActionView::Context` is used to provide access to the current controller, request, and helpers, which is why they cannot be referenced until a component is rendered.
40+
41+
First, ViewComponent calls the `before_render` method, which gives ViewComponents the opportunity to execute logic before render time but after initialization.
42+
43+
Second, ViewComponent calls `render?`, returning early with an empty string if `render?` returns false.
44+
45+
Last, ViewComponent renders the component.
46+

docs/known_issues.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Known issues
4-
nav_order: 9
4+
nav_order: 11
55
---
66

77
# Known issues

docs/logo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Logo
4-
nav_order: 11
4+
nav_order: 12
55
---
66

77
# Logo

docs/resources.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Resources
4-
nav_order: 12
4+
nav_order: 13
55
---
66

77
# Resources

0 commit comments

Comments
 (0)