Skip to content

Commit e76918a

Browse files
authored
fix(trainings): updated trainings and fixed linked codesandboxes (#4923)
1 parent 5b16138 commit e76918a

File tree

3 files changed

+153
-104
lines changed

3 files changed

+153
-104
lines changed

packages/documentation-site/patternfly-docs/content/get-started/training/html-css/css-variables-and-overrides.md

Lines changed: 73 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ section: get-started
44
subsection: training
55
hideNavItem: true
66
---
7-
import { Button, ClipboardCopy, Divider, PageSection } from '@patternfly/react-core';
7+
import { Button, Divider } from '@patternfly/react-core';
88

9-
<PageSection variant="light">
109
# CSS variables and overrides
11-
PatternFly Core is based on the principles of <a href="http://bradfrost.com/blog/post/atomic-web-design/" target="_blank">Atomic Design</a> and <a href="http://getbem.com/introduction/" target="_blank">BEM</a> (Block, Element, Modifier). BEM is a popular CSS methodology for building modular, scalable applications. It provides scope, avoids inheritance, and reduces CSS specificity and conflicts.
1210

13-
BEM works perfectly with a modular design system, as each unique component can be represented as an independent block. Since a block is tied to a component, developers are able to develop, move around, and nest components without conflicts in their application’s CSS.
11+
PatternFly is based on the principles of <a href="http://bradfrost.com/blog/post/atomic-web-design/" target="_blank">Atomic Design</a> and <a href="http://getbem.com/introduction/" target="_blank">BEM</a> (Block, Element, Modifier). BEM is a popular CSS methodology for building modular, scalable applications. It provides scope, avoids inheritance, and reduces CSS specificity and conflicts.
12+
13+
BEM works perfectly with a modular design system, as each unique component can be represented as an independent block. Since a block is tied to a component, developers are able to develop, move around, and nest components without conflicts in their application's CSS.
1414
PatternFly uses a modified version of BEM for its CSS architecture. PatternFly deviates from BEM in relation to modifiers.
1515

1616
This tutorial covers how PatternFly uses BEM as a framework for its component library. You'll learn how to override and create both global and component-level custom properties and component elements.
17-
</PageSection>
18-
<PageSection>
17+
1918
## Part 1: Overriding PatternFly component-level properties
19+
2020
In PatternFly, component-level custom properties follow this general formula:
2121
`--pf-v6-c-block[__element][--modifier][--state][--breakpoint][--pseudo-element]--PropertyCamelCase.`
2222
- `--pf-v6-c-block` refers to the block, usually the component or layout name (for example, `--pf-v6-c-alert`).
@@ -29,36 +29,43 @@ In PatternFly, component-level custom properties follow this general formula:
2929

3030
To explore this concept, you can practice overriding the title color custom property in the success variation of the alert component using the provided CodeSandbox.
3131

32-
<Button variant="primary" component="a" href="https://codesandbox.io/s/html-fundamentals-start-hy2erg" target="_blank">Codesandbox - Part 1</Button>
32+
<Button variant="primary" component="a" href="https://codesandbox.io/p/devbox/html-fundamentals-start-forked-dll7lt?workspaceId=ws_SgkMrETzDgBAttxqLiNZfT" target="_blank">Codesandbox - Part 1</Button>
3333

3434
### Step 1. Familiarize with `index.html`
35+
3536
Note the alert component in the `<body>` of `index.html`.
3637

3738
### Step 2. Create custom property name and styles
38-
Write the CSS for the custom property name in the `style.css` file. Overriding the success variation’s title color means overriding its custom property.
3939

40-
#### Step 2.1
40+
Write the CSS for the custom property name in the `style.css` file. Overriding the success variation's title color means overriding its custom property.
41+
42+
#### Step 2.1
43+
4144
In the `style.css` file, in the `.pf-v6-c-alert{}` block, write the custom property name.
4245

4346
In reference to the formula described in Part 1, this should be: `--pf-v6-c-alert`
4447

45-
#### Step 2.2
48+
#### Step 2.2
49+
4650
Add the modifier to the custom property name. As displayed in the CSS variables of PatternFly's <a href="/components/alert/html#css-variables" target="_blank">alert component documentation</a>, the success variation modifier class `pf-m-success` applies to `pf-v6-c-alert`. Add that modifier to the custom property.
4751

4852
The custom property name should now be: `--pf-v6-c-alert--m-success`
4953

50-
#### Step 2.3
54+
#### Step 2.3
55+
5156
Add the element to the custom property name. The element that is being changed is the title of the alert.
5257

5358
The custom property name should now be: `--pf-v6-c-alert--m-success__title`
5459

55-
#### Step 2.4
60+
#### Step 2.4
61+
5662
Add the property that is being modified. In this case, modify the color property of the title in the alert component.
5763

5864
The custom property name should now be: `--pf-v6-c-alert--m-success__title--Color`
5965

60-
#### Step 2.5
61-
Define the value of the component-level CSS variable. PatternFly's global danger color is: `--pf-v6-global--danger-color--100`. You can reference PatternFly’s <a href="/foundations-and-styles/colors" target="_blank">global colors documentation</a> for more information.
66+
#### Step 2.5
67+
68+
Define the value of the component-level CSS variable. PatternFly's global danger color is: `--pf-v6-global--danger-color--100`. You can reference PatternFly's <a href="/foundations-and-styles/colors" target="_blank">global colors documentation</a> for more information.
6269

6370
Assign the custom property name `(--pf-v6-c-alert--m-success__title--Color)` that is already inside the `.pf-v6-c-alert{}` block to the global danger color.
6471

@@ -67,11 +74,13 @@ It should look like this: `--pf-v6-c-alert--m-success__title--Color: var(--pf-v6
6774
The resulting alert should match Figure 1.
6875

6976
**Figure 1**
77+
7078
<img src="../img/alert-red-title.png" alt="Alert component with a red title" width="868" />
71-
</PageSection>
79+
7280
<Divider />
73-
<PageSection>
81+
7482
## Part 2: Overriding PatternFly global properties
83+
7584
In PatternFly, global properties follow this general formula:
7685
`--pf-v6-global--concept--PropertyCamelCase--modifier--state`
7786

@@ -86,136 +95,160 @@ To explore this concept, override the global link color by setting it to the cus
8695
<Button variant="primary" component="a" href="https://codesandbox.io/s/override-global-css-variables-start-llcub8" target="_blank">Codesandbox - Part 2</Button>
8796

8897
### Step 1. Familiarize with `index.html`
98+
8999
Note the button component and the expandable section component in the `<body>` of the `index.html` file.
90100

91101
### Step 2. Override a global custom property
92102

93-
#### Step 2.1
103+
#### Step 2.1
104+
94105
Write the prefix for the global custom property in the `:root` block in the `styles.css` file. Since global custom properties are prefixed with the word global, use that for the beginning of the custom property.
95106

96107
It should look like this: `--pf-v6-global`
97108

98-
#### Step 2.2
109+
#### Step 2.2
110+
99111
Add the concept to the global custom property. The concept for the link color is `link`.
100112

101113
The global custom property should now look like this: `--pf-v6-global--link`
102114

103-
#### Step 2.3
115+
#### Step 2.3
116+
104117
Add the property to the global custom property. As the color is being modified, add the color property as the next step.
105118

106119
The global custom property should now look like: `--pf-v6-global--link--Color`
107120

108-
#### Step 2.4
121+
#### Step 2.4
122+
109123
Define the value of the global variable. PatternFly's global danger color is: `--pf-v6-global--danger-color--100`.
110124
Assign the custom property name `(--pf-v6-global--link--Color)` that is already inside of the `:root` block to the global danger color.
111125

112126
It should look like this: `--pf-v6-global--link--Color: var(--pf-v6-global--danger-color--100);`
113127

114-
The resulting UI in the CodeSandbox should match Figure 2.
128+
The resulting UI in the CodeSandbox should match Figure 2.
115129

116130
**Note:** The links in the button component and expandable component both have inherited the new red link color.
117131

118132
**Figure 2.**
119133

120134
<img src="../img/global-link-color-red.png" alt="An inline button and a 'show more' expandable section toggle both colored red" width="300" />
121-
</PageSection>
135+
122136
<Divider />
123-
<PageSection>
137+
124138
## Part 3: Create and use component level CSS variables to override PatternFly styles
139+
125140
It is important to understand how BEM is used to create and override variables.
126141

127142
To explore this concept, add a BEM element and custom styles with CSS custom properties to support a badge element in the label component using the provided CodeSandbox for part 3. Assume there is a common use case for adding a badge to the left of text in the label component. When adding the custom badge element to the label component, also apply a margin-right of 8px.
128143

129144
<Button variant="primary" component="a" href="https://codesandbox.io/s/creating-new-component-css-vars-start-t98i06" target="_blank">Codesandbox - Part 3</Button>
130145

131146
### Step 1. Familiarize with `index.html`
147+
132148
Note the label component in the `<body>` of the `index.html`. This label contains a badge component and some text. The default PatternFly label component only styles the label itself and the text inside. Since there has been a badge component passed into the label, it is necessary to add space between the badge and the text beside it.
133149

134150
To do this the BEM way, a BEM element class `(pf-v6-c-label__badge)` has been applied to the badge.
135151

136152
### Step 2. Style the badge
153+
137154
**Note:** Never apply a global custom property as the value for a property in a component's CSS.
138155

139-
#### Step 2.1
156+
#### Step 2.1
157+
140158
Create a new variable to represent the badge's margin. Within style.css, in the `.pf-v6-c-label{}` block, define the beginning of the custom property, which is the name of the component.
141159

142160
It should look like: `--pf-v6-c-label`
143161

144-
#### Step 2.2
162+
#### Step 2.2
163+
145164
Define the value of the new custom property. Add the element after the name of the component.
146165

147166
The custom property should now be: `--pf-v6-c-label__badge`
148167

149-
#### Step 2.3
168+
#### Step 2.3
169+
150170
Add the property being applied to the badge after the element.
151171

152172
It should look like this: `--pf-v6-c-label__badge--MarginRight`
153173

154-
#### Step 2.4
155-
Define the value of the new custom CSS variable. PatternFly's global variable for 8px of space is `--pf-v6-global--spacer--sm`. You can reference <a href="/foundations-and-styles/spacers" target="_blank">PatternFly’s documentation about spacers</a> for more information.
174+
#### Step 2.4
175+
176+
Define the value of the new custom CSS variable. PatternFly's global variable for 8px of space is `--pf-v6-global--spacer--sm`. You can reference <a href="/foundations-and-styles/spacers" target="_blank">PatternFly's documentation about spacers</a> for more information.
156177

157178
Assign the custom property name `--pf-v6-c-label__badge--MarginRight` that is already inside of the `.pf-v6-c-label{}` block to the global variable for 8px of space.
158179

159180
It should look like this: `--pf-v6-c-label__badge--MarginRight: var(--pf-v6-global--spacer--sm);`
160181

161182
#### Step 2.5
183+
162184
Assign the new custom property name to the property that is being overridden. Add a margin-right declaration inside of `.pf-v6-c-label__badge{}` and assign it to the new margin variable.
163185

164186
It should look like this: `margin-right: var(--pf-v6-c-label__badge--MarginRight);`
165187

166188
**Note:** Once the preview reloads, there should be space to the right of the badge.
167-
</PageSection>
189+
168190
<Divider />
169-
<PageSection>
191+
170192
## Part 4: Create and use global CSS variables to override PatternFly styles
193+
171194
To explore this concept, create a new global custom property for a 5xl font size. Use this new global property to make a new variation of the title component using the provided CodeSandbox for part 4.
172195

173196
Referencing <a href="/foundations-and-styles/typography" target="_blank">PatternFly's typography design guidelines</a>, the largest font size PatternFly offers is a "super hero heading" size. It is 36px represented by the global variable `pf-v6-global--FontSize--4xl`. Assume that there is a need for a larger font size for the title component that is used across the entire application.
174197

175198
<Button variant="primary" component="a" href="https://codesandbox.io/s/creating-new-global-css-vars-start-cqb8nw" target="_blank">Codesandbox - Part 4</Button>
176199

177200
### Step 1. Familiarize with `index.html`
201+
178202
Note the title component in the `<body>` of the `index.html` file. It has a modifier class applied to it which has no PatternFly styles defined for it thus far.
179203

180204
### Step 2. Define a global custom property name
205+
181206
Follow the existing convention for global font size custom properties.
182207

183208
#### Step 2.1
209+
184210
Add the new custom property for font size inside of the `:root{}` block. This applies it to the global scope of the application. It should look like this: `--pf-v6-global--FontSize--5xl`
185211

186-
#### Step 2.2
212+
#### Step 2.2
213+
187214
Define the value of the new global variable. Assign the property name to the pixel value for the new font size.
188215
Update the line added in step 2.1 to be 42px.
189216

190217
It should look like this: `--pf-v6-global--FontSize--5xl: 42px;`
191218

192219
### Step 3. Create a local component level custom property
220+
193221
Create a local component level custom property in the `.pf-v6-c-title{}` block in the styles.css file. Set its value to the newly defined global property.
194222
The local custom property should represent the 5xl variation's font size.
195223

196-
#### Step 3.1
224+
#### Step 3.1
225+
197226
Beginning with the component name, write the custom property and add it to the `.pf-v6-c-title{}` block in the `style.css` file.
198227

199228
It should look like this: `--pf-v6-c-title`
200229

201-
#### Step 3.2
230+
#### Step 3.2
231+
202232
Add the new modifier to the custom property name.
203233

204234
It should look like this: `--pf-v6-c-title--m-5xl`
205235

206-
#### Step 3.3
236+
#### Step 3.3
237+
207238
Add the property that the custom property represents.
208239

209240
It should look like this: `--pf-v6-c-title--m-5xl--FontSize`
210241

211-
#### Step 3.4
242+
#### Step 3.4
243+
212244
Define the value of the component level custom property to be equal to the newly defined global variable.
213245

214246
It should look like this: `--pf-v6-c-title--m-5xl--FontSize: var(--pf-v6-global--FontSize--5xl);`
215247

216248
### Step 4. Define the styles for the new 5xl variation.
217249

218250
#### Step 4.1 Chain selectors
251+
219252
Knowing that the modifier `.pf-m-5xl` will apply to the title component, in the CSS, chain the selector to create `.pf-v6-c-title.pf-m-5xl`
220253
Add this block inside of `.pf-v6-c-title`, underneath the declaration from step 3.
221254

@@ -225,15 +258,16 @@ Add this block inside of `.pf-v6-c-title`, underneath the declaration from step
225258
}
226259
```
227260

228-
#### Step 4.2
261+
#### Step 4.2
262+
229263
Create a declaration within the modifier block for the font-size CSS rule using the new component level custom property.
230264

231265
It should look like: `font-size: var(--pf-v6-c-title--m-5xl--FontSize);` inside of `.pf-m-5xl{}`.
232-
</PageSection>
266+
233267
<Divider />
234-
<PageSection>
268+
235269
### Compare your results.
270+
236271
A fully constructed card can be viewed and modified in the CodeSandbox solution for part 4. Compare your work with the solution.
237272

238273
<Button variant="primary" component="a" href="https://codesandbox.io/s/creating-new-global-css-vars-final-cugbq0?file=/style.css" target="_blank">Codesandbox - Part 4 Solution</Button>
239-
</PageSection>

0 commit comments

Comments
 (0)