Skip to content

Commit

Permalink
Document features automatically. (#2780)
Browse files Browse the repository at this point in the history
* document features automatically.

* Document with `#[cfg(...)]`.

* Fix Clippy.

* Adjust unreleased docs header.

* Adapt dark mode as well.
  • Loading branch information
futursolo committed Jul 18, 2022
1 parent 423ef28 commit 6f53a4e
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 48 deletions.
4 changes: 2 additions & 2 deletions api-docs/before-content.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="unreleased-version-header">
<p>This is unreleased documentation for Yew Next version.</p>
<p>For up-to-date documentation, see <a href="https://docs.rs/yew">the latest version on docs.rs</a>.</p>
<div>This is unreleased documentation for Yew Next version.</div>
<div>For up-to-date documentation, see <a href="https://docs.rs/yew">the latest version on docs.rs</a>.</div>
</div>
14 changes: 12 additions & 2 deletions api-docs/styles.css
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
#unreleased-version-header {
background-color: rgb(77, 56, 0);
background-color: rgb(200, 237, 248);
z-index: 400;
position: fixed;
position: absolute;
left: 0;
top: 0;
right: 0;
height: 70px;
padding-top: 10px;
padding-bottom: 10px;
text-align: center;
font-family: sans-serif;
box-shadow: 0 0 5px 0 rgb(100, 100, 100);
}

@media (prefers-color-scheme: dark) {
#unreleased-version-header {
background-color: rgb(32, 43, 57);
box-shadow: 0 0 5px 0 black;
}
}

body {
Expand Down
3 changes: 1 addition & 2 deletions packages/yew/src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::dom_bundle::BSubtree;
use crate::html::{BaseComponent, NodeRef, Scope, Scoped};

/// An instance of an application.
#[cfg_attr(documenting, doc(cfg(feature = "csr")))]
#[cfg(feature = "csr")]
#[derive(Debug)]
pub struct AppHandle<COMP: BaseComponent> {
/// `Scope` holder
Expand Down Expand Up @@ -65,7 +65,6 @@ fn clear_element(host: &Element) {
}
}

#[cfg_attr(documenting, doc(cfg(feature = "hydration")))]
#[cfg(feature = "hydration")]
mod feat_hydration {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/dom_bundle/subtree_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ static BUBBLE_EVENTS: AtomicBool = AtomicBool::new(true);
/// handler has no effect.
///
/// This function should be called before any component is mounted.
#[cfg_attr(documenting, doc(cfg(feature = "csr")))]
#[cfg(feature = "csr")]
pub fn set_event_bubbling(bubble: bool) {
BUBBLE_EVENTS.store(bubble, Ordering::Relaxed);
}
Expand Down
8 changes: 0 additions & 8 deletions packages/yew/src/functional/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,21 @@ mod use_context;
mod use_effect;
mod use_force_update;
mod use_memo;
#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))]
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
mod use_prepared_state;
mod use_reducer;
mod use_ref;
mod use_state;
#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))]
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
mod use_transitive_state;

pub use use_callback::*;
pub use use_context::*;
pub use use_effect::*;
pub use use_force_update::*;
pub use use_memo::*;
#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))]
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
pub use use_prepared_state::*;
pub use use_reducer::*;
pub use use_ref::*;
pub use use_state::*;
#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))]
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
pub use use_transitive_state::*;

use crate::functional::HookContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ pub use feat_ssr::*;
/// Whilst async closure is an unstable feature, the procedural macro will rewrite this to a
/// closure that returns an async block automatically. You can use this hook with async closure
/// in stable Rust.
#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))]
pub use use_prepared_state_macro as use_prepared_state;
// With SSR.
#[doc(hidden)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ pub use feat_ssr::*;
/// You MUST denote the return type of the closure with `|deps| -> ReturnType { ... }`. This
/// type is used during client side rendering to deserialize the state prepared on the server
/// side.
#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))]
pub use use_transitive_state_macro as use_transitive_state;
// With SSR.
#[doc(hidden)]
Expand Down
28 changes: 3 additions & 25 deletions packages/yew/src/functional/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use std::rc::Rc;

use wasm_bindgen::prelude::*;

#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(all(feature = "hydration", feature = "ssr"))]
use crate::html::RenderMode;
use crate::html::{AnyScope, BaseComponent, Context, HtmlResult};
Expand Down Expand Up @@ -68,7 +67,6 @@ pub use yew_macro::hook;
type ReRender = Rc<dyn Fn()>;

/// Primitives of a prepared state hook.
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(any(feature = "hydration", feature = "ssr"))]
pub(crate) trait PreparedState {
#[cfg(feature = "ssr")]
Expand All @@ -83,22 +81,18 @@ pub(crate) trait Effect {
/// A hook context to be passed to hooks.
pub struct HookContext {
pub(crate) scope: AnyScope,
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(all(feature = "hydration", feature = "ssr"))]
creation_mode: RenderMode,
re_render: ReRender,

states: Vec<Rc<dyn Any>>,
effects: Vec<Rc<dyn Effect>>,

#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(any(feature = "hydration", feature = "ssr"))]
prepared_states: Vec<Rc<dyn PreparedState>>,

#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(feature = "hydration")]
prepared_states_data: Vec<Rc<str>>,
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(feature = "hydration")]
prepared_state_counter: usize,

Expand All @@ -111,37 +105,29 @@ impl HookContext {
fn new(
scope: AnyScope,
re_render: ReRender,
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(all(feature = "hydration", feature = "ssr"))]
creation_mode: RenderMode,
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(feature = "hydration")]
prepared_state: Option<&str>,
#[cfg(all(feature = "hydration", feature = "ssr"))] creation_mode: RenderMode,
#[cfg(feature = "hydration")] prepared_state: Option<&str>,
) -> RefCell<Self> {
RefCell::new(HookContext {
scope,
re_render,

#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(all(feature = "hydration", feature = "ssr"))]
creation_mode,

states: Vec::new(),

#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(any(feature = "hydration", feature = "ssr"))]
prepared_states: Vec::new(),
effects: Vec::new(),

#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(feature = "hydration")]
prepared_states_data: {
match prepared_state {
Some(m) => m.split(',').map(Rc::from).collect(),
None => Vec::new(),
}
},
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(feature = "hydration")]
prepared_state_counter: 0,

Expand Down Expand Up @@ -187,7 +173,6 @@ impl HookContext {
t
}

#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(any(feature = "hydration", feature = "ssr"))]
pub(crate) fn next_prepared_state<T>(
&mut self,
Expand Down Expand Up @@ -220,7 +205,6 @@ impl HookContext {

#[inline(always)]
fn prepare_run(&mut self) {
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(feature = "hydration")]
{
self.prepared_state_counter = 0;
Expand Down Expand Up @@ -277,15 +261,11 @@ impl HookContext {
}
}

#[cfg(any(
not(feature = "ssr"),
not(any(target_arch = "wasm32", feature = "tokio"))
))]
#[cfg(not(feature = "ssr"))]
fn prepare_state(&self) -> Option<String> {
None
}

#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(feature = "ssr")]
fn prepare_state(&self) -> Option<String> {
if self.prepared_states.is_empty() {
Expand Down Expand Up @@ -361,10 +341,8 @@ where
hook_ctx: HookContext::new(
scope,
re_render,
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(all(feature = "hydration", feature = "ssr"))]
ctx.creation_mode(),
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
#[cfg(feature = "hydration")]
ctx.prepared_state(),
),
Expand Down
1 change: 0 additions & 1 deletion packages/yew/src/html/component/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,6 @@ mod feat_csr {
#[cfg(feature = "csr")]
pub(crate) use feat_csr::*;

#[cfg_attr(documenting, doc(cfg(feature = "hydration")))]
#[cfg(feature = "hydration")]
mod feat_hydration {
use wasm_bindgen::JsCast;
Expand Down
1 change: 1 addition & 0 deletions packages/yew/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::needless_doctest_main)]
#![doc(html_logo_url = "https://yew.rs/img/logo.png")]
#![cfg_attr(documenting, feature(doc_cfg))]
#![cfg_attr(documenting, feature(doc_auto_cfg))]
#![cfg_attr(
feature = "nightly",
feature(fn_traits, async_closure, unboxed_closures)
Expand Down
5 changes: 2 additions & 3 deletions packages/yew/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ thread_local! {
/// Set a custom panic hook.
/// Unless a panic hook is set through this function, Yew will
/// overwrite any existing panic hook when an application is rendered with [Renderer].
#[cfg_attr(documenting, doc(cfg(feature = "csr")))]
#[cfg(feature = "csr")]
pub fn set_custom_panic_hook(hook: Box<dyn Fn(&PanicInfo<'_>) + Sync + Send + 'static>) {
std::panic::set_hook(hook);
PANIC_HOOK_IS_SET.with(|hook_is_set| hook_is_set.set(true));
Expand All @@ -29,7 +29,7 @@ fn set_default_panic_hook() {
/// The Yew Renderer.
///
/// This is the main entry point of a Yew application.
#[cfg_attr(documenting, doc(cfg(feature = "csr")))]
#[cfg(feature = "csr")]
#[derive(Debug)]
#[must_use = "Renderer does nothing unless render() is called."]
pub struct Renderer<COMP>
Expand Down Expand Up @@ -93,7 +93,6 @@ where
}
}

#[cfg_attr(documenting, doc(cfg(feature = "hydration")))]
#[cfg(feature = "hydration")]
mod feat_hydration {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions packages/yew/src/server_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::platform::io::{self, DEFAULT_BUF_SIZE};
use crate::platform::{run_pinned, spawn_local};

/// A Yew Server-side Renderer that renders on the current thread.
#[cfg_attr(documenting, doc(cfg(feature = "ssr")))]
#[cfg(feature = "ssr")]
#[derive(Debug)]
pub struct LocalServerRenderer<COMP>
where
Expand Down Expand Up @@ -112,7 +112,7 @@ where
/// the rendering process has finished.
///
/// See [`yew::platform`] for more information.
#[cfg_attr(documenting, doc(cfg(feature = "ssr")))]
#[cfg(feature = "ssr")]
pub struct ServerRenderer<COMP>
where
COMP: BaseComponent,
Expand Down

0 comments on commit 6f53a4e

Please sign in to comment.