leptos_use_breakpoints/use_breakpoints.rs
1use crate::UseBreakpointsReturn;
2use std::collections::HashMap;
3use std::fmt::Debug;
4use std::hash::Hash;
5
6/// Reactive viewport breakpoints.
7///
8/// ## Demo
9///
10/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_breakpoints)
11///
12/// ## Usage
13///
14/// ```
15/// # use leptos::prelude::*;
16/// # use leptos_use_breakpoints::{use_breakpoints, BreakpointsTailwind, breakpoints_tailwind};
17/// #
18/// # #[component]
19/// # fn Demo() -> impl IntoView {
20/// #
21/// let screen_width = use_breakpoints(breakpoints_tailwind());
22///
23/// use BreakpointsTailwind::*;
24///
25/// let sm_and_larger = screen_width.ge(Sm);
26/// let larger_than_sm = screen_width.gt(Sm);
27/// let lg_and_smaller = screen_width.le(Lg);
28/// let smaller_than_lg = screen_width.lt(Lg);
29/// #
30/// # view! { }
31/// # }
32/// ```
33///
34/// ## Breakpoints
35///
36/// There are many predefined breakpoints for major UI frameworks. The following are provided.
37///
38/// * [`breakpoints_tailwind`]
39/// * [`breakpoints_bootstrap_v5`]
40/// * [`breakpoints_material`]
41/// * [`breakpoints_ant_design`]
42/// * [`breakpoints_quasar`]
43/// * [`breakpoints_semantic`]
44/// * [`breakpoints_master_css`]
45///
46/// You can also provide your own breakpoints.
47///
48/// ```
49/// # use std::collections::HashMap;
50/// use leptos::prelude::*;
51/// # use leptos_use_breakpoints::use_breakpoints;
52/// #
53/// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
54/// enum MyBreakpoints {
55/// Tablet,
56/// Laptop,
57/// Desktop,
58/// }
59///
60/// fn my_breakpoints() -> HashMap<MyBreakpoints, u32> {
61/// use MyBreakpoints::*;
62///
63/// HashMap::from([
64/// (Tablet, 640),
65/// (Laptop, 1024),
66/// (Desktop, 1280),
67/// ])
68/// }
69///
70/// #[component]
71/// fn Demo() -> impl IntoView {
72/// let screen_width = use_breakpoints(my_breakpoints());
73///
74/// use MyBreakpoints::*;
75///
76/// let laptop = screen_width.between(Laptop, Desktop);
77///
78/// view! { }
79/// }
80/// ```
81///
82/// ## Non-reactive methods
83///
84/// For every reactive method there is also a non-reactive variant that is prefixed with `is_`
85///
86/// ```
87/// # use leptos::prelude::*;
88/// # use leptos_use_breakpoints::{use_breakpoints, BreakpointsTailwind, breakpoints_tailwind};
89/// #
90/// # #[component]
91/// # fn Demo() -> impl IntoView {
92/// #
93/// let screen_width = use_breakpoints(breakpoints_tailwind());
94///
95/// use BreakpointsTailwind::*;
96///
97/// let sm_and_larger = screen_width.is_ge(Sm);
98/// let larger_than_sm = screen_width.is_gt(Sm);
99/// let lg_and_smaller = screen_width.is_le(Lg);
100/// let smaller_than_lg = screen_width.is_lt(Lg);
101/// #
102/// # view! { }
103/// # }
104/// ```
105///
106/// ## Server-Side Rendering
107///
108/// > Make sure you follow the [instructions in Server-Side Rendering](https://leptos-use.rs/server_side_rendering.html).
109///
110/// Since internally this uses [`fn@crate::use_media_query`], which returns always `false` on the server,
111/// the returned methods also will return `false`.
112pub fn use_breakpoints<K: Eq + Hash + Debug + Clone + Send + Sync>(
113 breakpoints: HashMap<K, u32>,
114) -> UseBreakpointsReturn<K> {
115 UseBreakpointsReturn { breakpoints }
116}
117
118/// Breakpoint keys for Tailwind V2
119///
120/// See <https://tailwindcss.com/docs/breakpoints>
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
122pub enum BreakpointsTailwind {
123 Sm,
124 Md,
125 Lg,
126 Xl,
127 Xxl,
128}
129
130/// Breakpoint definitions for Tailwind V2
131///
132/// See <https://tailwindcss.com/docs/breakpoints>
133pub fn breakpoints_tailwind() -> HashMap<BreakpointsTailwind, u32> {
134 HashMap::from([
135 (BreakpointsTailwind::Sm, 640),
136 (BreakpointsTailwind::Md, 768),
137 (BreakpointsTailwind::Lg, 1024),
138 (BreakpointsTailwind::Xl, 1280),
139 (BreakpointsTailwind::Xxl, 1536),
140 ])
141}
142
143/// Breakpoint keys for Bootstrap V5
144///
145/// See <https://getbootstrap.com/docs/5.0/layout/breakpoints>
146#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
147pub enum BreakpointsBootstrapV5 {
148 Sm,
149 Md,
150 Lg,
151 Xl,
152 Xxl,
153}
154
155/// Breakpoint definitions for Bootstrap V5
156///
157/// <https://getbootstrap.com/docs/5.0/layout/breakpoints>
158pub fn breakpoints_bootstrap_v5() -> HashMap<BreakpointsBootstrapV5, u32> {
159 HashMap::from([
160 (BreakpointsBootstrapV5::Sm, 576),
161 (BreakpointsBootstrapV5::Md, 768),
162 (BreakpointsBootstrapV5::Lg, 992),
163 (BreakpointsBootstrapV5::Xl, 1200),
164 (BreakpointsBootstrapV5::Xxl, 1400),
165 ])
166}
167
168/// Breakpoint keys for Material UI V5
169///
170/// See <https://mui.com/material-ui/customization/breakpoints/>
171#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
172pub enum BreakpointsMaterial {
173 Xs,
174 Sm,
175 Md,
176 Lg,
177 Xl,
178}
179
180/// Breakpoint definitions for Material UI V5
181///
182/// See <https://mui.com/material-ui/customization/breakpoints/>
183pub fn breakpoints_material() -> HashMap<BreakpointsMaterial, u32> {
184 HashMap::from([
185 (BreakpointsMaterial::Xs, 1),
186 (BreakpointsMaterial::Sm, 600),
187 (BreakpointsMaterial::Md, 900),
188 (BreakpointsMaterial::Lg, 1200),
189 (BreakpointsMaterial::Xl, 1536),
190 ])
191}
192
193/// Breakpoint keys for Ant Design
194///
195/// See <https://ant.design/components/layout/#breakpoint-width>
196#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
197pub enum BreakpointsAntDesign {
198 Xs,
199 Sm,
200 Md,
201 Lg,
202 Xl,
203 Xxl,
204}
205
206/// Breakpoint definitions for Ant Design
207///
208/// See <https://ant.design/components/layout/#breakpoint-width>
209pub fn breakpoints_ant_design() -> HashMap<BreakpointsAntDesign, u32> {
210 HashMap::from([
211 (BreakpointsAntDesign::Xs, 480),
212 (BreakpointsAntDesign::Sm, 576),
213 (BreakpointsAntDesign::Md, 768),
214 (BreakpointsAntDesign::Lg, 992),
215 (BreakpointsAntDesign::Xl, 1200),
216 (BreakpointsAntDesign::Xxl, 1600),
217 ])
218}
219
220/// Breakpoint keys for Quasar V2
221///
222/// See <https://quasar.dev/style/breakpoints>
223#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
224pub enum BreakpointsQuasar {
225 Xs,
226 Sm,
227 Md,
228 Lg,
229 Xl,
230}
231
232/// Breakpoint definitions for Quasar V2
233///
234/// See <https://quasar.dev/style/breakpoints>
235pub fn breakpoints_quasar() -> HashMap<BreakpointsQuasar, u32> {
236 HashMap::from([
237 (BreakpointsQuasar::Xs, 1),
238 (BreakpointsQuasar::Sm, 600),
239 (BreakpointsQuasar::Md, 1024),
240 (BreakpointsQuasar::Lg, 1440),
241 (BreakpointsQuasar::Xl, 1920),
242 ])
243}
244
245/// Breakpoint keys for Semantic UI
246///
247/// See <https://semantic-ui.com/elements/container.html>
248#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
249pub enum BreakpointsSemantic {
250 Mobile,
251 Tablet,
252 SmallMonitor,
253 LargeMonitor,
254}
255
256/// Breakpoint definitions for Semantic UI
257///
258/// See <https://semantic-ui.com/elements/container.html>
259pub fn breakpoints_semantic() -> HashMap<BreakpointsSemantic, u32> {
260 HashMap::from([
261 (BreakpointsSemantic::Mobile, 1),
262 (BreakpointsSemantic::Tablet, 768),
263 (BreakpointsSemantic::SmallMonitor, 992),
264 (BreakpointsSemantic::LargeMonitor, 1200),
265 ])
266}
267
268/// Breakpoint keys for Master CSS
269///
270/// See <https://docs.master.co/css/breakpoints>
271#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
272pub enum BreakpointsMasterCss {
273 Xxxs,
274 Xxs,
275 Xs,
276 Sm,
277 Md,
278 Lg,
279 Xl,
280 Xxl,
281 Xxxl,
282 Xxxxl,
283}
284
285/// Breakpoint definitions for Master CSS
286///
287/// See <https://docs.master.co/css/breakpoints>
288pub fn breakpoints_master_css() -> HashMap<BreakpointsMasterCss, u32> {
289 HashMap::from([
290 (BreakpointsMasterCss::Xxxs, 360),
291 (BreakpointsMasterCss::Xxs, 480),
292 (BreakpointsMasterCss::Xs, 600),
293 (BreakpointsMasterCss::Sm, 768),
294 (BreakpointsMasterCss::Md, 1024),
295 (BreakpointsMasterCss::Lg, 1280),
296 (BreakpointsMasterCss::Xl, 1440),
297 (BreakpointsMasterCss::Xxl, 1600),
298 (BreakpointsMasterCss::Xxxl, 1920),
299 (BreakpointsMasterCss::Xxxxl, 2560),
300 ])
301}