Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,270 --> 00:00:03,440
Now, wherever you like
2
00:00:03,440 --> 00:00:06,800
styled components or not as an absolutely personal thing
3
00:00:06,800 --> 00:00:09,490
there is no wrong or right here.
4
00:00:09,490 --> 00:00:11,330
Now, when it comes to me personally
5
00:00:11,330 --> 00:00:14,590
I prefer CSS in the CSS files.
6
00:00:14,590 --> 00:00:18,070
I like the separation of CSS and JavaScript.
7
00:00:18,070 --> 00:00:21,210
I like having clean JavaScript files.
8
00:00:21,210 --> 00:00:24,603
And I simply like default CSS.
9
00:00:25,500 --> 00:00:27,570
Now, of course you can absolutely stick
10
00:00:27,570 --> 00:00:30,270
to the CSS approach we used at the beginning
11
00:00:30,270 --> 00:00:33,270
where you had global non scoped styles
12
00:00:33,270 --> 00:00:35,950
and you as a developer had to make sure
13
00:00:35,950 --> 00:00:39,420
you're not accidentally reusing names, class names.
14
00:00:39,420 --> 00:00:41,580
You're not accidentally affecting the styles
15
00:00:41,580 --> 00:00:42,940
of (mumbles) components.
16
00:00:42,940 --> 00:00:45,510
That is definitely one route you can take.
17
00:00:45,510 --> 00:00:48,650
You can also however take CSS to the next level
18
00:00:48,650 --> 00:00:51,313
with a feature called CSS Modules.
19
00:00:52,420 --> 00:00:56,160
CSS Modules is a feature which is only available
20
00:00:56,160 --> 00:00:59,920
in projects that are configured to support it
21
00:00:59,920 --> 00:01:04,160
because it needs a code transformation that needs to be done
22
00:01:04,160 --> 00:01:06,660
before your code runs in the browser.
23
00:01:06,660 --> 00:01:09,870
Now, the good thing is the react projects created
24
00:01:09,870 --> 00:01:12,760
with create react app which we used
25
00:01:12,760 --> 00:01:16,690
are already configured to support CSS Modules.
26
00:01:16,690 --> 00:01:17,670
This is actually an excerpt
27
00:01:17,670 --> 00:01:20,010
from the official create react app docs
28
00:01:20,010 --> 00:01:23,800
where you'll learn how you can utilize CSS Modules.
29
00:01:23,800 --> 00:01:27,960
And it's super, super simple to utilize CSS Modules.
30
00:01:27,960 --> 00:01:30,310
So let me show you how it works
31
00:01:30,310 --> 00:01:33,120
and let's start with the good old button again.
32
00:01:33,120 --> 00:01:36,660
Now of course you can stick to the style component there
33
00:01:36,660 --> 00:01:40,360
but I will comment this out and comment out this import
34
00:01:40,360 --> 00:01:45,360
and instead add the import all for react from react again
35
00:01:46,020 --> 00:01:50,700
because I will comment in my own component again,
36
00:01:50,700 --> 00:01:52,940
this one which uses JSX
37
00:01:52,940 --> 00:01:55,240
and which (mumbles) for needs to react import.
38
00:01:56,250 --> 00:01:58,700
Now we have the button CSS fall in India.
39
00:01:58,700 --> 00:02:02,690
We got the different styles and we can leave that as it is.
40
00:02:02,690 --> 00:02:05,600
There's one thing we now need to change though.
41
00:02:05,600 --> 00:02:09,509
Of course, we need to add the import to the CSS file again,
42
00:02:09,509 --> 00:02:12,320
but instead of importing it like this
43
00:02:12,320 --> 00:02:15,130
you now import it slightly different.
44
00:02:15,130 --> 00:02:20,130
You import classes or styles totally up to you
45
00:02:20,460 --> 00:02:22,960
from the CSS file.
46
00:02:22,960 --> 00:02:25,110
That's a syntax we haven't used before,
47
00:02:25,110 --> 00:02:29,450
but that is actually how you have to import from a CSS file
48
00:02:29,450 --> 00:02:32,053
if you wanna use CSS Modules.
49
00:02:32,920 --> 00:02:35,900
Actually for (mumbles) code transformation,
50
00:02:35,900 --> 00:02:38,410
which is needed behind the scenes to happen,
51
00:02:38,410 --> 00:02:40,930
you also need to rename your file a little bit,
52
00:02:40,930 --> 00:02:42,430
your CSS file.
53
00:02:42,430 --> 00:02:45,350
You need to add dot module in there
54
00:02:45,350 --> 00:02:50,350
so that it's named Button.module.CSS.
55
00:02:50,510 --> 00:02:51,510
And then of course here,
56
00:02:51,510 --> 00:02:55,070
you import from Button.module.CSS.
57
00:02:55,070 --> 00:02:56,600
This is basically a signal
58
00:02:56,600 --> 00:02:59,630
to the underlying compilation process
59
00:02:59,630 --> 00:03:03,410
to transform to code so that CSS Modules work.
60
00:03:03,410 --> 00:03:04,690
And I'll show you how they work
61
00:03:04,690 --> 00:03:06,190
behind the scenes in a second.
62
00:03:07,130 --> 00:03:09,880
So now we have this import and now we need to do something
63
00:03:09,880 --> 00:03:13,350
with this import, that styles thing here.
64
00:03:13,350 --> 00:03:16,800
We use it down there where we apply our class name.
65
00:03:16,800 --> 00:03:20,140
Instead of applying a string class named like this
66
00:03:20,140 --> 00:03:22,480
you apply something dynamic.
67
00:03:22,480 --> 00:03:24,680
You refer to that styles thing
68
00:03:24,680 --> 00:03:27,170
which you're importing from the CSS file.
69
00:03:27,170 --> 00:03:30,220
And that thing turns out to be an object.
70
00:03:30,220 --> 00:03:34,390
And in that object, you'll have every class you defined
71
00:03:34,390 --> 00:03:37,170
in your file as a property.
72
00:03:37,170 --> 00:03:40,550
So if I added a button class here in the CSS file
73
00:03:40,550 --> 00:03:43,583
I now have the button property here.
74
00:03:44,840 --> 00:03:46,310
And that's all we need to do.
75
00:03:46,310 --> 00:03:51,310
This will apply this button class in a special way
76
00:03:51,680 --> 00:03:53,793
to this button element here.
77
00:03:54,930 --> 00:03:57,540
Let's see it an actual let's save this
78
00:03:57,540 --> 00:03:59,870
and go back to our application.
79
00:03:59,870 --> 00:04:03,333
And we see the button looks the way it looked before.
80
00:04:04,370 --> 00:04:05,910
Now we see something interesting
81
00:04:05,910 --> 00:04:08,150
if we inspect this here in the Def tools.
82
00:04:08,150 --> 00:04:11,170
On the button you'll see a very strange class.
83
00:04:11,170 --> 00:04:15,450
This is not the same class as added by style components.
84
00:04:15,450 --> 00:04:19,209
Those classes looked like this here on the div.
85
00:04:19,209 --> 00:04:24,210
Instead this looks a bit like the class we defined, button,
86
00:04:24,580 --> 00:04:27,370
but with more information added to it.
87
00:04:27,370 --> 00:04:31,243
It's basically component name, underscore, our class name
88
00:04:31,243 --> 00:04:36,243
and the CSS file, double underscore some unique hash.
89
00:04:37,480 --> 00:04:39,800
What CSS Modules does
90
00:04:39,800 --> 00:04:42,300
or what this concept of CSS Modules does.
91
00:04:42,300 --> 00:04:45,060
What the build process does under the hood
92
00:04:45,060 --> 00:04:49,260
is it takes those CSS classes and a CSS file
93
00:04:49,260 --> 00:04:51,630
and basically changes those class names
94
00:04:51,630 --> 00:04:53,170
to be unique.
95
00:04:53,170 --> 00:04:54,660
That's the core thing it does.
96
00:04:54,660 --> 00:04:57,550
For every component it changes the class names
97
00:04:57,550 --> 00:05:00,190
off the classes you're importing here.
98
00:05:00,190 --> 00:05:03,760
So off the CSS file you were importing to be unique.
99
00:05:03,760 --> 00:05:06,040
So if you import from the CSS file here
100
00:05:06,040 --> 00:05:07,560
in the button component,
101
00:05:07,560 --> 00:05:10,940
it will create unique classes, unique worshens
102
00:05:10,940 --> 00:05:15,280
of all those styles and classes here for this component.
103
00:05:15,280 --> 00:05:17,640
And for data it generates brand new class names
104
00:05:17,640 --> 00:05:19,320
that look like this.
105
00:05:19,320 --> 00:05:22,250
Inside of these class names, your styles will be
106
00:05:22,250 --> 00:05:24,030
so it will just keep your styles.
107
00:05:24,030 --> 00:05:25,750
It will not touch those
108
00:05:25,750 --> 00:05:28,470
but wrapped into this new class name.
109
00:05:28,470 --> 00:05:30,770
You can see this if you expand the head section here
110
00:05:30,770 --> 00:05:32,440
in the rendered Dom.
111
00:05:32,440 --> 00:05:35,040
There you will see a bunch of style texts.
112
00:05:35,040 --> 00:05:37,270
And if we now search for the one here,
113
00:05:37,270 --> 00:05:38,740
here at this for the button
114
00:05:38,740 --> 00:05:41,823
we see that's this newly generated class name.
115
00:05:42,770 --> 00:05:44,490
Here we have our button styles
116
00:05:44,490 --> 00:05:47,390
and that newly generated class name is also used
117
00:05:47,390 --> 00:05:50,663
on the focus style on the hover and on the active style.
118
00:05:51,790 --> 00:05:55,960
And with that, does CSS Modules concept ensures
119
00:05:55,960 --> 00:06:00,130
that the CSS styles we set up in a CSS file
120
00:06:00,130 --> 00:06:02,600
are a scoped Q and a component
121
00:06:02,600 --> 00:06:05,210
we import this file into.
122
00:06:05,210 --> 00:06:08,460
For (mumbles) we need to work with CSS classes here
123
00:06:08,460 --> 00:06:10,400
because we then access those classes
124
00:06:10,400 --> 00:06:13,723
as properties on the imported styles object.
125
00:06:14,610 --> 00:06:16,440
This is how we basically make the connection
126
00:06:16,440 --> 00:06:19,780
between these dynamically generated class names,
127
00:06:19,780 --> 00:06:22,560
which we as a developer don't know in advance
128
00:06:22,560 --> 00:06:24,233
and our components here.
129
00:06:26,450 --> 00:06:29,367
And that's also what you see here in the official docs.
130
00:06:29,367 --> 00:06:31,550
That's in the end, all that's to it.
131
00:06:31,550 --> 00:06:34,120
This is how you can use CSS Modules.
132
00:06:34,120 --> 00:06:37,030
And for me that's actually the best of both worlds.
133
00:06:37,030 --> 00:06:40,600
We have our styles set up in CSS files
134
00:06:40,600 --> 00:06:43,963
but there's still scoped to components where we use them.
135
00:06:44,970 --> 00:06:46,370
So in the next module,
136
00:06:46,370 --> 00:06:50,970
let's all support the course input back to use the CSS file.
137
00:06:50,970 --> 00:06:54,400
And then I will also show you how to use media queries
138
00:06:54,400 --> 00:06:56,033
with CSS Modules.
10998
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.