﻿1
00:00:01,140 --> 00:00:04,380
‫One important part of building web applications

2
00:00:04,380 --> 00:00:07,950
‫is, of course, to style them with CSS.

3
00:00:07,950 --> 00:00:10,710
‫Now, up until this point, we have always just

4
00:00:10,710 --> 00:00:15,540
‫included a global external CSS file into our applications,

5
00:00:15,540 --> 00:00:20,220
‫and then used the class names from there in our JSX.

6
00:00:20,220 --> 00:00:22,170
‫However, in this project,

7
00:00:22,170 --> 00:00:24,390
‫we will do something a bit different,

8
00:00:24,390 --> 00:00:27,450
‫which is to use CSS modules.

9
00:00:27,450 --> 00:00:30,000
‫But before we do that, I want to take a minute

10
00:00:30,000 --> 00:00:32,670
‫to quickly explore all the different options

11
00:00:32,670 --> 00:00:35,700
‫that we can use to style React applications

12
00:00:35,700 --> 00:00:38,733
‫because there are actually a lot of them.

13
00:00:40,350 --> 00:00:43,380
‫But first you might be wondering, why are there

14
00:00:43,380 --> 00:00:48,180
‫actually so many different ways of styling a React app?

15
00:00:48,180 --> 00:00:52,110
‫Well, it's because one fundamental philosophy of React

16
00:00:52,110 --> 00:00:55,020
‫is to be unopinionated in regards

17
00:00:55,020 --> 00:00:59,070
‫to many common aspects of building web applications.

18
00:00:59,070 --> 00:01:02,190
‫And so one of them is styling.

19
00:01:02,190 --> 00:01:04,260
‫So React really doesn't care

20
00:01:04,260 --> 00:01:06,990
‫about how you style your applications.

21
00:01:06,990 --> 00:01:08,550
‫And so as a result,

22
00:01:08,550 --> 00:01:11,160
‫we have lots of different styling options,

23
00:01:11,160 --> 00:01:15,390
‫most of them being provided by third party libraries.

24
00:01:15,390 --> 00:01:17,730
‫So the first option is one

25
00:01:17,730 --> 00:01:20,250
‫that we have used a few times in the beginning,

26
00:01:20,250 --> 00:01:23,220
‫which is to simply apply some inline CSS

27
00:01:23,220 --> 00:01:27,367
‫to JSX elements using this style prop.

28
00:01:27,367 --> 00:01:29,310
‫Now, this is actually more common in React

29
00:01:29,310 --> 00:01:31,200
‫than in regular HTML

30
00:01:31,200 --> 00:01:35,433
‫because of React's idea of separation of concerns.

31
00:01:36,390 --> 00:01:38,940
‫Now, an inline style is scoped

32
00:01:38,940 --> 00:01:42,750
‫to the particular JSX element that it's applied to,

33
00:01:42,750 --> 00:01:45,660
‫which means that it is locally scoped,

34
00:01:45,660 --> 00:01:49,860
‫so it applies only to that exact element.

35
00:01:49,860 --> 00:01:52,230
‫Now, we have also multiple times

36
00:01:52,230 --> 00:01:54,900
‫included an external CSS file,

37
00:01:54,900 --> 00:01:57,720
‫and then simply applied the CSS classes

38
00:01:57,720 --> 00:02:00,180
‫using the class name prop.

39
00:02:00,180 --> 00:02:01,830
‫And the same would actually

40
00:02:01,830 --> 00:02:05,130
‫also have worked for a Sass file.

41
00:02:05,130 --> 00:02:08,880
‫Now, in this case, our styles are actually global,

42
00:02:08,880 --> 00:02:11,880
‫which means that every single JSX element

43
00:02:11,880 --> 00:02:15,870
‫in the entire application could use any of these classes

44
00:02:15,870 --> 00:02:18,510
‫in the external CSS file.

45
00:02:18,510 --> 00:02:20,820
‫And this can create huge problems,

46
00:02:20,820 --> 00:02:23,250
‫especially in big projects,

47
00:02:23,250 --> 00:02:25,350
‫for example, because you won't know

48
00:02:25,350 --> 00:02:28,470
‫which components are using which classes.

49
00:02:28,470 --> 00:02:30,990
‫And when you then update one of the classes,

50
00:02:30,990 --> 00:02:34,530
‫it will have repercussions in other components,

51
00:02:34,530 --> 00:02:37,050
‫or when a developer adds a new class

52
00:02:37,050 --> 00:02:39,210
‫with a name that already exists,

53
00:02:39,210 --> 00:02:43,350
‫that will create clashes between those two classes.

54
00:02:43,350 --> 00:02:48,350
‫So basically, global CSS is a nightmare in large apps.

55
00:02:48,570 --> 00:02:53,570
‫So in professional projects, CSS is almost never global.

56
00:02:53,970 --> 00:02:58,560
‫Instead, CSS should be scoped to an individual component,

57
00:02:58,560 --> 00:03:01,380
‫which brings us to the next styling options,

58
00:03:01,380 --> 00:03:03,843
‫which is CSS modules.

59
00:03:04,680 --> 00:03:09,090
‫CSS modules are pretty similar to regular CSS files

60
00:03:09,090 --> 00:03:12,450
‫with the difference that we write just one CSS file

61
00:03:12,450 --> 00:03:15,180
‫for each of our components.

62
00:03:15,180 --> 00:03:17,880
‫The styles in that file will then be scoped

63
00:03:17,880 --> 00:03:19,830
‫to only that component

64
00:03:19,830 --> 00:03:22,830
‫so that no other component can use them.

65
00:03:22,830 --> 00:03:24,660
‫And this then makes the components

66
00:03:24,660 --> 00:03:27,240
‫way more modular and reusable.

67
00:03:27,240 --> 00:03:28,650
‫And at the same time,

68
00:03:28,650 --> 00:03:32,670
‫it better reflects React's separation of concerns.

69
00:03:32,670 --> 00:03:34,020
‫And in fact,

70
00:03:34,020 --> 00:03:38,310
‫this is exactly what we will do in this project.

71
00:03:38,310 --> 00:03:41,190
‫Now, if you want to take it even one step further,

72
00:03:41,190 --> 00:03:44,550
‫you can go with a CSS in JavaScript library

73
00:03:44,550 --> 00:03:46,083
‫like styled components.

74
00:03:46,920 --> 00:03:50,700
‫So as the name says with CSS in JavaScript,

75
00:03:50,700 --> 00:03:55,590
‫you actually write your CSS inside a JavaScript file,

76
00:03:55,590 --> 00:03:59,103
‫so in the same file where you define your components.

77
00:03:59,970 --> 00:04:03,480
‫What's special about a CSS in JavaScript library

78
00:04:03,480 --> 00:04:06,480
‫is that it allows us to create React components

79
00:04:06,480 --> 00:04:09,510
‫that have our styles directly apply to them,

80
00:04:09,510 --> 00:04:13,170
‫which we can then use just like regular components.

81
00:04:13,170 --> 00:04:16,020
‫So this fully embraces the React philosophy

82
00:04:16,020 --> 00:04:19,080
‫that a component should contain all the information

83
00:04:19,080 --> 00:04:22,713
‫about its appearance, and so that includes CSS.

84
00:04:23,640 --> 00:04:28,470
‫And finally, you can also use a utility-first CSS framework

85
00:04:28,470 --> 00:04:33,470
‫like Tailwind, which is getting more popular every day.

86
00:04:33,480 --> 00:04:37,260
‫So in Tailwind, you use predefined utility classes

87
00:04:37,260 --> 00:04:40,620
‫to define individual styles, to use flexbox,

88
00:04:40,620 --> 00:04:44,130
‫to make layouts responsive, to make hover effects,

89
00:04:44,130 --> 00:04:47,190
‫and really to design your entire UI,

90
00:04:47,190 --> 00:04:51,513
‫and all that without ever having to leave the JSX markup.

91
00:04:52,740 --> 00:04:54,720
‫Okay, now finally,

92
00:04:54,720 --> 00:04:57,930
‫you do actually have one more option here,

93
00:04:57,930 --> 00:05:02,550
‫which is basically to not write any CSS at all.

94
00:05:02,550 --> 00:05:05,580
‫Wait, what? No CSS?

95
00:05:05,580 --> 00:05:07,680
‫Well, it is actually possible,

96
00:05:07,680 --> 00:05:10,200
‫because you can build your entire project

97
00:05:10,200 --> 00:05:13,860
‫using a fully fledged UI component library,

98
00:05:13,860 --> 00:05:18,860
‫for example, like Material UI, Chakra UI, or Mantine.

99
00:05:18,900 --> 00:05:22,050
‫So essentially, a component library like those

100
00:05:22,050 --> 00:05:26,790
‫contains all kinds of prebuilt and pre-styled components

101
00:05:26,790 --> 00:05:30,210
‫that are common in most web applications.

102
00:05:30,210 --> 00:05:33,300
‫This is, however, not ideal for beginners,

103
00:05:33,300 --> 00:05:36,630
‫but again, it might be worth exploring later.

104
00:05:36,630 --> 00:05:38,010
‫And there you have it.

105
00:05:38,010 --> 00:05:39,900
‫So these are the different ways

106
00:05:39,900 --> 00:05:42,990
‫in which we can style React applications.

107
00:05:42,990 --> 00:05:46,773
‫And so in this project, we will now explore CSS modules.

