forked from learningequality/kolibri-design-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusekresponsivewindow.vue
192 lines (167 loc) · 4.8 KB
/
usekresponsivewindow.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<template>
<DocsPageTemplate apiDocs>
<DocsPageSection
title="Overview"
anchor="#overview"
>
<p>Provides the following reactive window's size information:</p>
<dl>
<dt>
<code>windowIsSmall</code>, <code>windowIsMedium</code>, and <code>windowIsLarge</code>
</dt>
<dd>
Returns <code>true</code> when the window width fits the small, medium, or large
breakpoint respectively as defined in
<DocsInternalLink
text="Layout: Responsiveness"
href="/layout#responsiveness"
/>
(boolean)
</dd>
<dt><code>windowHeight</code></dt>
<dd>Returns the window height in pixels (integer)</dd>
<dt><code>windowWidth</code></dt>
<dd>Returns the window width in pixels (integer)</dd>
<dt><code>windowBreakpoint</code></dt>
<dd>
Returns one of the granular breakpoints defined as levels in
<DocsInternalLink
text="Layout: Responsiveness"
href="/layout#responsiveness"
/>
(integer, 0-7)
</dd>
</dl>
<p>
Provided reactive properties are typically used to dynamically drive the layout of
components by adjusting inline styles, CSS classes, component visibility, or even swapping
out one component for a completely different one.
</p>
</DocsPageSection>
<DocsPageSection
title="Usage"
anchor="#usage"
>
<!-- eslint-disable -->
<!-- prettier-ignore -->
<DocsShowCode language="javascript">
import useKResponsiveWindow from 'kolibri-design-system/lib/composables/useKResponsiveWindow';
export default {
setup() {
const {
windowIsSmall,
windowIsMedium,
windowIsLarge,
windowHeight,
windowWidth,
windowBreakpoint
} = useKResponsiveWindow();
}
};
</DocsShowCode>
<!-- eslint-enable -->
</DocsPageSection>
<DocsPageSection
title="Example"
anchor="#example"
>
<p>Consider a Vue file with the following template and script:</p>
<!-- eslint-disable -->
<!-- prettier-ignore -->
<DocsShowCode language="html">
<div
class="box"
:style="boxStyle"
>
Box 1
</div>
<div
class="box"
:style="boxStyle"
>
Box 2
</div>
</DocsShowCode>
<!-- prettier-ignore -->
<DocsShowCode language="javascript">
import { computed } from '@vue/composition-api';
import useKResponsiveWindow from 'kolibri-design-system/lib/composables/useKResponsiveWindow';
export default {
setup() {
const {
windowIsLarge
} = useKResponsiveWindow();
const boxStyle = computed(function () {
return { display: windowIsLarge.value ? 'inline-block' : 'block' };
});
return {
boxStyle,
};
}
};
</DocsShowCode>
<!-- eslint-enable -->
<p>
This results in two boxes that stack vertically on small screens and otherwise display
side-by-side:
</p>
<DocsShow>
<div>Window is large: {{ windowIsLarge }}</div>
<div>
<div
class="box"
:style="boxStyle"
>
Box 1
</div>
<div
class="box"
:style="boxStyle"
>
Box 2
</div>
</div>
</DocsShow>
<p>Try adjusting your browser window size to see the example in action.</p>
</DocsPageSection>
<DocsPageSection
title="Related"
anchor="#related"
>
<ul>
<li>
<DocsInternalLink
text="Layout: Responsiveness"
href="/layout#responsiveness"
/>
has an overview of breakpoints
</li>
<li>
See <DocsLibraryLink component="useKResponsiveElement" /> if you need a component's size
reactive information rather than that of the window
</li>
</ul>
</DocsPageSection>
</DocsPageTemplate>
</template>
<script>
import { computed } from 'vue';
import useKResponsiveWindow from '../../lib/composables/useKResponsiveWindow';
export default {
setup() {
const { windowIsLarge } = useKResponsiveWindow();
const boxStyle = computed(() => {
return { display: windowIsLarge.value ? 'inline-block' : 'block' };
});
return { windowIsLarge, boxStyle };
},
};
</script>
<style lang="scss" scoped>
.box {
padding: 16px;
margin-top: 8px;
margin-right: 8px;
border: 1px solid gray;
}
</style>