You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorial/08-bootstrap-jss.md
+58-53
Original file line number
Diff line number
Diff line change
@@ -2,40 +2,44 @@
2
2
3
3
Code for this chapter available in the [`master-no-services`](https://github.com/verekia/js-stack-boilerplate/tree/master-no-services) branch of the [JS-Stack-Boilerplate repository](https://github.com/verekia/js-stack-boilerplate).
4
4
5
-
Alright! It's time to give our ugly app a facelift. We are going to use Twitter Bootstrap to give it some base styles. We'll then add a CSS-in-JS library to add some custom styles.
There are 2 options to integrate Bootstrap in a React app. Both have their pros and cons:
11
+
在React App 內,有兩個選項可以整合 Bootstrap。兩者皆各有優缺點:
12
12
13
-
-Using the official release, **which uses jQuery and Tether** for the behavior of its components.
14
-
-Using a third-party library that re-implements all of Bootstrap's components in React, like [React-Bootstrap](https://react-bootstrap.github.io/) or [Reactstrap](https://reactstrap.github.io/).
Third-party libraries provide very convenient React components that dramatically reduce the code bloat compared to the official HTML components, and integrate greatly with your React codebase. That being said, I must say that I am quite reluctant to use them, because they will always be *behind* the official releases (sometimes potentially far behind). They also won't work with Bootstrap themes that implement their own JS. That's a pretty tough drawback considering that one major strength of Bootstrap is its huge community of designers who make beautiful themes.
For this reason, I'm going to make the tradeoff of integrating the official release, alongside with jQuery and Tether. One of the concerns of this approach is the file size of our bundle of course. For your information, the bundle weights about 200KB (Gzipped) with jQuery, Tether, and Bootstrap's JS included. I think that's reasonable, but if that's too much for you, you should probably consider an other option for Bootstrap, or even not using Bootstrap at all.
Alright, it's time for you to copy-paste a whole bunch of files.
59
+
好吧,到了複製貼上文件的時候了。
56
60
57
-
-Edit`src/shared/component/page/hello-async.jsx`like so:
61
+
-編輯`src/shared/component/page/hello-async.jsx`如下:
58
62
59
63
```js
60
64
// @flow
@@ -88,7 +92,7 @@ const HelloAsyncPage = () =>
88
92
exportdefaultHelloAsyncPage
89
93
```
90
94
91
-
-Edit`src/shared/component/page/hello.jsx`like so:
95
+
-編輯`src/shared/component/page/hello.jsx`如下:
92
96
93
97
```js
94
98
// @flow
@@ -122,7 +126,7 @@ const HelloPage = () =>
122
126
exportdefaultHelloPage
123
127
```
124
128
125
-
-Edit`src/shared/component/page/home.jsx`like so:
129
+
-編輯`src/shared/component/page/home.jsx`如下:
126
130
127
131
```js
128
132
// @flow
@@ -169,7 +173,7 @@ const HomePage = () =>
169
173
exportdefaultHomePage
170
174
```
171
175
172
-
-Edit`src/shared/component/page/not-found.jsx`like so:
176
+
-編輯`src/shared/component/page/not-found.jsx`如下:
173
177
174
178
```js
175
179
// @flow
@@ -195,7 +199,7 @@ const NotFoundPage = () =>
195
199
exportdefaultNotFoundPage
196
200
```
197
201
198
-
-Edit`src/shared/component/button.jsx`like so:
202
+
-編輯`src/shared/component/button.jsx`如下:
199
203
200
204
```js
201
205
// [...]
@@ -208,7 +212,7 @@ export default NotFoundPage
208
212
// [...]
209
213
```
210
214
211
-
-Create a `src/shared/component/footer.jsx` file containing:
215
+
-建立文件: `src/shared/component/footer.jsx`
212
216
213
217
```js
214
218
// @flow
@@ -227,7 +231,7 @@ const Footer = () =>
227
231
exportdefaultFooter
228
232
```
229
233
230
-
-Create a `src/shared/component/modal-example.jsx` containing:
234
+
-建立文件: `src/shared/component/modal-example.jsx`
231
235
232
236
```js
233
237
// @flow
@@ -255,18 +259,19 @@ const ModalExample = () =>
255
259
exportdefaultModalExample
256
260
```
257
261
258
-
-Edit`src/shared/app.jsx`like so:
262
+
-編輯`src/shared/app.jsx`如下:
259
263
260
264
```js
261
265
constApp= () =>
262
266
<div style={{ paddingTop:54 }}>
263
267
```
264
268
265
-
This is an example of a *React inline style*.
269
+
這是一個 *React的行內樣式範例*。
266
270
267
-
This will translate into: `<div style="padding-top:54px;">` in your DOM. We need this style to push the content under the navigation bar, but that's what's important here. [React inline styles](https://speakerdeck.com/vjeux/react-css-in-js) are a great way to isolate your component's styles from the global CSS namespace, but it comes at a price: You cannot use some native CSS features like `:hover`, Media Queries, animations, or `font-face`. That's the reason why we're going to integrate a CSS-in-JS library, JSS, later in this chapter. For now, just keep in mind that you can use React inline styles this way if you don't need `:hover` and such.
There is something new here, `handleNavLinkClick`. One issue I encountered using Bootstrap's `navbar`in an SPA is that clicking on a link on mobile does not collapse the menu, and does not scroll back to the top of the page. This is a great opportunity to show you an example of how you would integrate some jQuery / Bootstrap-specific code in your app:
**Note**: I've removed accessibility-related attributes (like `aria`attributes) to make the code more readable *in the context of this tutorial*. **You should absolutely put them back**. Refer to Bootstrap's documentation and code samples to see how to use them.
🏁 Your app should now be entirely styled with Bootstrap.
336
+
🏁 您的app設計現在應該完全如同 Bootstrap。
332
337
333
-
## The current state of CSS
338
+
## CSS的當前狀態
334
339
335
-
In 2016, the typical modern JavaScript stack settled. The different libraries and tools this tutorial made you set up are pretty much the *cutting-edge industry standard* (*cough – even though it could become completely outdated in a year from now – cough*). Yes, that's a complex stack to set up, but at least, most front-end devs agree that React-Redux-Webpack is the way to go. Now regarding CSS, I have some pretty bad news. Nothing settled, there is no standard way to go, no standard stack.
SASS, BEM, SMACSS, SUIT, Bass CSS, React Inline Styles, LESS, Styled Components, CSSX, JSS, Radium, Web Components, CSS Modules, OOCSS, Tachyons, Stylus, Atomic CSS, PostCSS, Aphrodite, React Native for Web, and many more that I forget are all different approaches or tools to get the job done. They all do it well, which is the problem, there is no clear winner, it's a big mess.
The cool React kids tend to favor React inline styles, CSS-in-JS, or CSS Modules approaches though, since they integrate really well with React and solve programmatically many [issues](https://speakerdeck.com/vjeux/react-css-in-js)that regular CSS approaches struggle with.
CSS Modules work well, but they don't leverage the power of JavaScript and its many features over CSS. They just provide encapsulation, which is fine, but React inline styles and CSS-in-JS take styling to an other level in my opinion. My personal suggestion would be to use React inline styles for common styles (that's also what you have to use for React Native), and use a CSS-in-JS library for things like `:hover`and media queries.
346
+
css模組化工作更棒,因為但是它們並沒有利用JavaScript的強大功能和CSS上的許多功能,他們只提供封裝,這是很好的,但是React的行內樣式和CSS-in-JS 在我看來,是樣式設計到另一個層次。我個人的建議是,React的行內樣式,應該使用於一般常用的樣式(這就是你必須使用於React Native),並且使用一個 CSS-in-JS 函式庫,就像 `:hover`與 media queries。
342
347
343
-
There are tons of CSS-in-JS libraries. Two leading ones are Aphrodite and JSS. They achieve pretty much the same thing and the syntax is basically the same. To be honest, I haven't done a comparison of the two on any significant-size project, and really just have a slight preference for JSS' API. We can discuss this topic in [this issue](https://github.com/verekia/js-stack-from-scratch/issues/139). I would like to hear the opinion of those who have done a more thorough comparison.
Now that we have some base template with Bootstrap, let's write some custom CSS. I mentioned earlier that React inline styles could not handle `:hover` and media queries, so we'll show a simple example of this on the homepage using JSS. JSS can be used via `react-jss`, a library that is convenient to use with React components.
354
+
現在我們有一些Bootstrap基本的模版,我們來寫一些自定義的css樣式。剛才提到React行內樣式無法控制譬如 `:hover`與 media queries,所以我們使用JSS來展現一個簡單的網頁範例,JSS透過使用 `react-jss`,一個便於與React組件共同使用的函式庫。
350
355
351
-
-Run`yarn add react-jss`
356
+
-執行`yarn add react-jss`
352
357
353
-
Add the following to your `.flowconfig`file, as there is currently a Flow [issue](https://github.com/cssinjs/jss/issues/411) with JSS:
🏁 Run`yarn start`and`yarn dev:wds`. Open the homepage. Show the source of the page (not in the inspector) to see that the JSS styles are present in the DOM at the initial render in the `<style class="jss-ssr">`element (only on the Home page). They should be gone in the inspector, replaced by `<style type="text/css" data-jss data-meta="HomePage">`.
**Note**: In production mode, the `data-meta`is obfuscated. Sweet!
451
+
**筆記**: 在生產模式下, `data-meta`易混淆。貼心!
447
452
448
-
If you hover over the "Hover me" label, it should turn red. If you resize your browser window to be narrower than 800px, the "Resize your window" label should turn red.
453
+
如果您將鼠標懸停在"Hover me"標籤上,應變為紅色。如果您將瀏覽器寬度調整為小於800px,則"Resize your window"標籤應變為紅色。
449
454
450
-
Next section: [09 - Travis, Coveralls, Heroku](09-travis-coveralls-heroku.md#readme)
0 commit comments