Skip to content

Commit ce3216f

Browse files
committed
add docs + use flake8-idom-hooks on idom itself
1 parent 84b5e2a commit ce3216f

File tree

7 files changed

+96
-47
lines changed

7 files changed

+96
-47
lines changed

Diff for: docs/source/conf.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@
109109
# Add any paths that contain custom static files (such as style sheets) here,
110110
# relative to this directory. They are copied after the builtin static files,
111111
# so a file named "default.css" will overwrite the builtin "default.css".
112-
# html_static_path = ["static"]
112+
html_static_path = ["static"]
113+
114+
# These paths are either relative to html_static_path
115+
# or fully qualified paths (eg. https://...)
116+
html_css_files = ["css/interactive-widget.css"]
113117

114118
# Custom sidebar templates, must be a dictionary that maps document names
115119
# to template names.

Diff for: docs/source/exts/interactive_widget.py

-38
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,6 @@ def run(self):
1717
raw(
1818
"",
1919
f"""
20-
<style>
21-
.interactive {{
22-
-webkit-transition: 0.2s ease-out;
23-
-moz-transition: 0.2s ease-out;
24-
-o-transition: 0.2s ease-out;
25-
transition: 0.2s ease-out;
26-
}}
27-
.widget-container {{
28-
padding: 15px;
29-
background-color: #fcfcfc;
30-
min-height: 75px;
31-
}}
32-
.center-content {{
33-
display: flex;
34-
align-items: center;
35-
justify-content: center;
36-
}}
37-
.enable-widget-button {{
38-
padding: 10px;
39-
color: #ffffff !important;
40-
text-transform: uppercase;
41-
text-decoration: none;
42-
background: #3980b9;
43-
border: 2px solid #3980b9 !important;
44-
transition: all 0.2s ease 0s;
45-
box-shadow: 0 5px 10px grey;
46-
}}
47-
.enable-widget-button:hover {{
48-
color: #3980b9 !important;
49-
background: #ffffff;
50-
transition: all 0.2s ease 0s;
51-
}}
52-
.enable-widget-button:focus {{
53-
outline: 0 !important;
54-
transform: scale(0.98);
55-
transition: all 0.2s ease 0s;
56-
}}
57-
</style>
5820
<div>
5921
<div id="{container_id}" class="interactive widget-container center-content" style="" />
6022
<script async type="module">

Diff for: docs/source/life-cycle-hooks.rst

+50-7
Original file line numberDiff line numberDiff line change
@@ -301,20 +301,63 @@ hook alongside :ref:`use_effect` or in response to element event handlers.
301301
**Rules of Hooks**
302302
------------------
303303

304-
Under construction... for now refer to
305-
`React's documentation <https://reactjs.org/docs/hooks-rules.html>`_ on this topic.
304+
Hooks are just normal Python functions, but there's a bit of magic to them, and in order
305+
for that magic to work you've got to follow two rules. Thankfully we supply a
306+
`Flake8 Linter Plugin`_ to help enforce them.
306307

307-
.. note::
308308

309-
We're `working on a linter <https://github.com/idom-team/idom/issues/202>`_ to help
310-
enforce the rules.
309+
Only call hooks at the top level
310+
--------------------------------
311+
312+
**Don't call hooks inside loops, conditions, or nested functions.** Instead you must
313+
always call hooks at the top level of your functions. By adhering to this rule you
314+
ensure that hooks are always called in the exact same order. This fact is what allows
315+
IDOM to preserve the state of hooks between multiple calls to ``useState`` and
316+
``useEffect`` calls.
317+
318+
319+
Only call hooks from IDOM functions
320+
-----------------------------------
321+
322+
**Don't call hooks from regular Python functions.** Instead you should:
323+
324+
- ✅ Call Hooks from an element's render function.
325+
326+
- ✅ Call Hooks from another custom hook
327+
328+
Following this rule ensures stateful logic for IDOM element is always clearly
329+
separated from the rest of your codebase.
330+
331+
332+
Flake8 Plugin
333+
-------------
334+
335+
We provide a Flake8 plugin called `flake8-idom-hooks <Flake8 Linter Plugin>`_ that helps
336+
to enforce the two rules described above. You can ``pip`` install it directly, or with
337+
the ``lint`` extra for IDOM:
338+
339+
.. code-block:: bash
340+
341+
pip install idom[stable,lint]
342+
343+
Once installed running ``flake8`` on your could will start catching errors:
344+
345+
.. code-block:: bash
346+
347+
flake8 my_idom_elements.py
348+
349+
.. code-block:: text
350+
351+
./my_idom_elements:10:8 ROH102 hook 'use_effect' used inside if statement
352+
./my_idom_elements:23:4 ROH102 hook 'use_state' used outside element or hook definition
311353
354+
See the Flake8 docs for
355+
`more info <https://flake8.pycqa.org/en/latest/user/configuration.html>`__.
312356

313357
.. links
314358
.. =====
315359
316360
.. _React Hooks: https://reactjs.org/docs/hooks-reference.html
317361
.. _side effects: https://en.wikipedia.org/wiki/Side_effect_(computer_science)
318362
.. _memoization: https://en.wikipedia.org/wiki/Memoization
319-
.. _premature optimization: https://en.wikiquote.org/wiki/Donald_Knuth#Computer_Programming_as_an_Art_(1974)
320-
.. _gh issues: https://github.com/idom-team/idom/issues
363+
.. _Flake8 Linter Plugin: https://github.com/idom-team/flake8-idom-hooks

Diff for: docs/source/static/css/interactive-widget.css

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.interactive {
2+
-webkit-transition: 0.2s ease-out;
3+
-moz-transition: 0.2s ease-out;
4+
-o-transition: 0.2s ease-out;
5+
transition: 0.2s ease-out;
6+
}
7+
.widget-container {
8+
padding: 15px;
9+
background-color: #fcfcfc;
10+
min-height: 75px;
11+
}
12+
.center-content {
13+
display: flex;
14+
align-items: center;
15+
justify-content: center;
16+
}
17+
.enable-widget-button {
18+
padding: 10px;
19+
color: #ffffff !important;
20+
text-transform: uppercase;
21+
text-decoration: none;
22+
background: #3980b9;
23+
border: 2px solid #3980b9 !important;
24+
transition: all 0.2s ease 0s;
25+
box-shadow: 0 5px 10px grey;
26+
}
27+
.enable-widget-button:hover {
28+
color: #3980b9 !important;
29+
background: #ffffff;
30+
transition: all 0.2s ease 0s;
31+
}
32+
.enable-widget-button:focus {
33+
outline: 0 !important;
34+
transform: scale(0.98);
35+
transition: all 0.2s ease 0s;
36+
}

Diff for: requirements/extras.txt

+3
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ matplotlib
99
htm
1010
pyalect
1111
tagged
12+
13+
# extra=lint
14+
flake8-idom-hooks

Diff for: requirements/lint.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
black
22
flake8
33
pep8-naming
4+
flake8-idom-hooks

Diff for: setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ warn_unused_ignores = True
88
ignore = E203, E266, E501, W503, F811, N802
99
max-line-length = 88
1010
max-complexity = 18
11-
select = B,C,E,F,W,T4,B9,N
11+
select = B,C,E,F,W,T4,B9,N,ROH
1212
exclude =
1313
idom/client/app/node_modules/*
1414
docs/source/examples/*

0 commit comments

Comments
 (0)