官术网_书友最值得收藏!

The Sass approach

The second way is more flexible and accurate. It is preferable to create a new theme manually by Sass because the theme is better maintainable. The main CSS settings, such as font, colors, border radius, and many more can be done configurable by Sass variables. You can create a new theme by setting custom values for those variables. This is exactly the approach followed by PrimeNG. The mostly free themes were created in such manner.

Every theme has a separate folder with a Sass file where variables are set. The variables themselves are used in _theme.scss--shared file for all free themes. This file can be found under node_modules/primeng/resources/themes/, if you install PrimeNG as the dependency. Sometimes, you also need to set custom fonts or special settings for particular CSS selectors. You can overwrite default style rules with your own ones--just write them after importing _theme.scss. The general structure of the custom theme file looks like as follows:

<predefined Sass variables>

@import "primeng/resources/themes/theme";

<your custom style rules>

Let's create the following folder structure with three Sass files for a new crazy theme:

- src
- assets
- themes
- crazy
- fonts
...
- _variables.scss
- theme.scss

Sass variables can be copied from any other theme, such as Omega and placed in _variables.scss. Some of them get custom values as shown here:

$fontFamily: "Quicksand", "Trebuchet MS", Arial, Helvetica, sans-serif;
...

// Header
$headerBorderWidth: 2px;
$headerBorderColor: #f0a9df;
...

// Content
$contentBorderWidth: 2px;
$contentBorderColor: #ffafaf;
...

// Forms
$invalidInputBorderColor: #ff0000;
...

As you see, we would like to use a custom font Quicksand. You can download this font in the .otf format (OpenType Font) from this free resource: https://www.fontsquirrel.com/fonts/quicksand. For cross-browser support, we need fonts in four formats: .ttf, .eot, .woff, and .svg. There are many converter tools and one of these can be found at http://www.font2web.com, which allows converting any .otf file to the mentioned four formats. After conversion, custom fonts should be moved to the fonts folder and installed via the @font-face rule.

Furthermore, we want to have pink gradient colors for widget's header and red borders around invalid fields. All these custom rules are done in the theme file theme.scss. An excerpt from this file illustrates the idea:

@import 'variables';
@import "primeng/resources/themes/theme";

@font-face {
font-family: 'Quicksand';
src: url('fonts/Quicksand-Regular.eot');
url('fonts/Quicksand-Regular.woff') format('woff'),
url('fonts/Quicksand-Regular.ttf') format('truetype'),
url('fonts/Quicksand-Regular.svg') format('svg');
font-weight: normal;
font-style: normal;
}

.ui-widget-header {
background: linear-gradient(to bottom, #fffcfc 0%, #f0a9df 100%);
}

.ui-inputtext.ng-dirty.ng-invalid,
p-dropdown.ng-dirty.ng-invalid > .ui-dropdown,
... {
border-color: $invalidInputBorderColor;
}
The complete project with the crazy theme is available on GitHub at
https://github.com/ova2/angular-development-with-primeng/tree/master/chapter2/custom-theme.

The proposed structure allows to create as many themes as you want. But, how to compile theme.scss to theme.css? There are two ways to compile Sass to CSS:

  1. Install the Sass from the command line. The installation process is described on the Sass homepage (http://sass-lang.com/install). Note that you need preinstalled Ruby. Once Sass is installed, you can run sass theme.scss theme.css from your terminal.
  2. Use node-sass (https://github.com/sass/node-sass) under Node.js.

In the project on GitHub, we used node-sass along with autoprefixer (https://github.com/postcss/autoprefixer) and cssnano (http://cssnano.co). All required dependencies are installed locally:

npm install node-sass autoprefixer cssnano postcss postcss-cli --save-dev

Four handy npm scripts in package.json help to create the theme file:

"premakecss": "node-sass --include-path node_modules/ src/assets/themes/crazy/theme.scss -o src/assets/themes/crazy/",
"makecss": "postcss src/assets/themes/crazy/theme.css --use
autoprefixer -d src/assets/themes/crazy/",
"prebuild:css": "npm run makecss",
"build:css": "postcss src/assets/themes/crazy/theme.css --use cssnano
> src/assets/themes/crazy/theme.min.css"
The @import "primeng/resources/themes/theme" path is found thanks to the --include-path node_modules/ option, which sets the path to look for the imported files. This helps to avoid all the mess with relative paths.

The npm run build:css command will produce theme.min.css, which should be included on the page:

<link rel="stylesheet" type="text/css" href="src/assets/themes/crazy/theme.min.css"/>

The look and feel of the new theme is amazing:

主站蜘蛛池模板: 龙陵县| 大关县| 龙陵县| 峡江县| 泰宁县| 义马市| 溆浦县| 光山县| 贺兰县| 九寨沟县| 黄石市| 光泽县| 同江市| 玛多县| 汨罗市| 凌源市| 尼玛县| 慈溪市| 北碚区| 大悟县| 丽江市| 化隆| 虞城县| 宁津县| 霍城县| 临海市| 卢氏县| 济源市| 灵宝市| 封丘县| 宣城市| 赤壁市| 松滋市| 青岛市| 连城县| 安新县| 浮山县| 勃利县| 青田县| 乌恰县| 若尔盖县|