Skip to content

Commit

Permalink
feat!: add @taiga-ui/styles (#2320)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Aug 30, 2022
1 parent 12ada74 commit d1a40a5
Show file tree
Hide file tree
Showing 60 changed files with 2,013 additions and 1,241 deletions.
1 change: 1 addition & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"i18n": "projects/i18n",
"icons": "projects/icons",
"kit": "projects/kit",
"styles": "projects/styles",
"taiga-schematics": "projects/taiga-schematics",
"testing": "projects/testing"
}
Expand Down
2,744 changes: 1,632 additions & 1,112 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"@types/parse5": "^6.0.3",
"@types/webpack-env": "^1.17.0",
"angular-http-server": "^1.10.0",
"cpy-cli": "^4.1.0",
"cy2": "~2.0.0",
"cypress": "~10.0.3",
"eslint-plugin-decorator-position": "~5.0.1",
Expand Down
2 changes: 1 addition & 1 deletion projects/cdk/schematics/constants/file-globs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ const EXCLUDE_FILE_PATTERNS = [

const FILTERED_PATHS = `!(${EXCLUDE_DIRECTORIES})/**/!(${EXCLUDE_FILE_PATTERNS})`;
export const ALL_TS_FILES = `${FILTERED_PATHS}.ts`;
export const ALL_FILES = `${FILTERED_PATHS}.{html,ts}`;
export const ALL_FILES = `${FILTERED_PATHS}.{html,ts,less}`;
6 changes: 4 additions & 2 deletions projects/cdk/schematics/constants/taiga-styles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const TAIGA_GLOBAL_STYLE =
'node_modules/@taiga-ui/core/styles/taiga-ui-global.less';
export const TAIGA_GLOBAL_STYLE = [
'node_modules/@taiga-ui/core/styles/taiga-ui-local.less',
'node_modules/@taiga-ui/styles/taiga-ui-global.less',
];
export const TAIGA_THEME_STYLE = 'node_modules/@taiga-ui/core/styles/taiga-ui-theme.less';
export const TAIGA_THEME_FONTS = 'node_modules/@taiga-ui/core/styles/taiga-ui-fonts.less';
7 changes: 7 additions & 0 deletions projects/cdk/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ function addDependencies(tree: Tree, options: Schema): void {
});
});

if (options.addGlobalStyles) {
addPackageJsonDependency(tree, {
name: `@taiga-ui/styles`,
version: TAIGA_VERSION,
});
}

if (options.addSanitizer) {
addPackageJsonDependency(tree, {
name: '@tinkoff/ng-dompurify',
Expand Down
9 changes: 9 additions & 0 deletions projects/cdk/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
"$source": "projectName"
}
},
"addGlobalStyles": {
"description": "Setting up global styles",
"type": "boolean",
"default": false,
"x-prompt": {
"message": "Do you want to use global Taiga UI classes, such as 'tui-space', 'tui-skeleton', etc?",
"type": "confirmation"
}
},
"addDialogsModule": {
"description": "Setting up dialog module",
"type": "boolean",
Expand Down
1 change: 1 addition & 0 deletions projects/cdk/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface Schema {
readonly project: string;
readonly addons: ReadonlyArray<string>;
readonly addGlobalStyles: boolean;
readonly addAlertModule: boolean;
readonly addDialogsModule: boolean;
readonly addSanitizer: boolean;
Expand Down
11 changes: 9 additions & 2 deletions projects/cdk/schematics/ng-add/steps/add-taiga-styles.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import {Rule, Tree} from '@angular-devkit/schematics';
import {Schema} from '../schema';
import {addStylesToAngularJson} from '../../utils/add-styles';
import {TAIGA_THEME_FONTS, TAIGA_THEME_STYLE} from '../../constants/taiga-styles';
import {
TAIGA_GLOBAL_STYLE,
TAIGA_THEME_FONTS,
TAIGA_THEME_STYLE,
} from '../../constants/taiga-styles';

export function addTaigaStyles(options: Schema): Rule {
return async (_: Tree, context) => {
const taigaStyles = [TAIGA_THEME_STYLE, TAIGA_THEME_FONTS];
const taigaLocalStyles = [TAIGA_THEME_STYLE, TAIGA_THEME_FONTS];
const taigaStyles = options.addGlobalStyles
? [...taigaLocalStyles, ...TAIGA_GLOBAL_STYLE]
: taigaLocalStyles;

return addStylesToAngularJson(options, context, taigaStyles);
};
Expand Down
77 changes: 77 additions & 0 deletions projects/cdk/schematics/ng-add/tests/schematic-ng-add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('ng-add', () => {
it('should add main modules in package.json', async () => {
const options: Schema = {
addSanitizer: false,
addGlobalStyles: false,
addDialogsModule: false,
addAlertModule: false,
addons: [],
Expand All @@ -66,6 +67,7 @@ describe('ng-add', () => {
it('should add additional modules in package.json', async () => {
const options: Schema = {
addSanitizer: true,
addGlobalStyles: false,
addDialogsModule: false,
addAlertModule: false,
addons: ['addon-doc', 'addon-mobile'],
Expand Down Expand Up @@ -95,6 +97,40 @@ describe('ng-add', () => {
);
});

it('should add additional modules in package.json and global styles', async () => {
const options: Schema = {
addSanitizer: true,
addGlobalStyles: true,
addDialogsModule: false,
addAlertModule: false,
addons: ['addon-doc', 'addon-mobile'],
project: '',
};

const tree = await runner.runSchematicAsync('ng-add', options, host).toPromise();

expect(tree.readContent('package.json')).toEqual(
`{
"devDependencies": {
"@types/dompurify": "${DOMPURIFY_TYPES_VERSION}"
},
"dependencies": {
"@angular/cdk": "^13.0.0",
"@angular/core": "~13.0.0",
"@taiga-ui/addon-doc": "${TAIGA_VERSION}",
"@taiga-ui/addon-mobile": "${TAIGA_VERSION}",
"@taiga-ui/cdk": "${TAIGA_VERSION}",
"@taiga-ui/core": "${TAIGA_VERSION}",
"@taiga-ui/icons": "${TAIGA_VERSION}",
"@taiga-ui/kit": "${TAIGA_VERSION}",
"@taiga-ui/styles": "${TAIGA_VERSION}",
"@tinkoff/ng-dompurify": "${NG_DOMPURIFY_VERSION}",
"dompurify": "${DOMPURIFY_VERSION}"
}
}`,
);
});

it('should add assets and styles in angular.json', async () => {
const tree = await runner
.runSchematicAsync('ng-add-setup-project', {}, host)
Expand Down Expand Up @@ -167,9 +203,50 @@ describe('ng-add', () => {
}`);
});

it('should add global styles', async () => {
createAngularJson({stylesExist: true});
saveActiveProject();

const tree = await runner
.runSchematicAsync('ng-add-setup-project', {addGlobalStyles: true}, host)
.toPromise();

expect(tree.readContent('angular.json')).toEqual(`
{
"version": 1,
"defaultProject": "demo",
"projects": {
"demo": {
"architect": {
"build": {
"options": {
"main": "test/main.ts",
"styles": [
"node_modules/@taiga-ui/core/styles/taiga-ui-theme.less",
"node_modules/@taiga-ui/core/styles/taiga-ui-fonts.less",
"node_modules/@taiga-ui/core/styles/taiga-ui-local.less",
"node_modules/@taiga-ui/styles/taiga-ui-global.less",
"some.style"
],
"assets": [
{
"glob": "**/*",
"input": "node_modules/@taiga-ui/icons/src",
"output": "assets/taiga-ui/icons"
}
]
}
}
}
}
}
}`);
});

it('Should add Taiga-ui modules and providers to main module', async () => {
const options: Schema = {
addSanitizer: true,
addGlobalStyles: false,
addDialogsModule: true,
addAlertModule: true,
addons: [],
Expand Down
2 changes: 2 additions & 0 deletions projects/cdk/schematics/ng-update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {FINISH_SYMBOL, START_SYMBOL, titleLog} from '../utils/colored-log';
import {dateTimeMigrations} from './steps/migrate-date-time';
import {addStylesToAngularJson} from '../utils/add-styles';
import {TAIGA_THEME_FONTS} from '../constants/taiga-styles';
import {replaceStyles} from './steps/replace-styles';
import {ALL_FILES} from '../constants';
import {getExecutionTime} from '../utils/get-execution-time';

Expand Down Expand Up @@ -61,6 +62,7 @@ function main(_: Schema): Rule {
renameTypes();
replaceConstants();
replaceServices();
replaceStyles();
showWarnings(context);
migrateTemplates(fileSystem);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
getTemplateFromTemplateResource,
getTemplateOffset,
} from '../../utils/templates/template-resource';
import {addProviderToComponent} from 'ng-morph';
import {getNgComponents} from '../../utils/angular/ng-component';
import {addUniqueImport} from '../../utils/add-unique-import';
import {
Expand Down
16 changes: 16 additions & 0 deletions projects/cdk/schematics/ng-update/steps/replace-styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {getActiveProject} from 'ng-morph';

export function replaceStyles() {
getActiveProject()
?.getSourceFiles('**/**.less')
.forEach(sourceFile => {
const fullText = sourceFile.getFullText();

sourceFile.replaceWithText(
fullText.replace(
`@import '~@taiga-ui/core/styles/taiga-ui-global';`,
`@import '~@taiga-ui/core/styles/taiga-ui-local';\n@import '~@taiga-ui/styles/taiga-ui-global';`,
),
);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export class AppComponent {}`,
);

expect(tree.readContent('test/style.less')).toEqual(
`@import '~@taiga-ui/core/styles/taiga-ui-global';`,
`@import '~@taiga-ui/core/styles/taiga-ui-local';
@import '~@taiga-ui/styles/taiga-ui-global';`,
);

expect(tree.readContent('test/app/app.template.less')).toEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';

import {
createProject,
createSourceFile,
resetActiveProject,
saveActiveProject,
setActiveProject,
} from 'ng-morph';
import {join} from 'path';
import {createAngularJson} from '../../utils/create-angular-json';

const collectionPath = join(__dirname, '../../migration.json');

const BEFORE_GLOBAL_STYLE = `
@import '~@taiga-ui/core/styles/taiga-ui-fonts';
@import '~@taiga-ui/core/styles/taiga-ui-global';
@import '~@taiga-ui/proprietary-core/styles/tinkoff-fonts';
@import '~@taiga-ui/proprietary-core/styles/theme-tinkoff-v2';
@import '~@taiga-ui/proprietary-core/styles/theme-tinkoff-mobile';
`;

const AFTER_GLOBAL_STYLE = `
@import '~@taiga-ui/core/styles/taiga-ui-fonts';
@import '~@taiga-ui/core/styles/taiga-ui-local';
@import '~@taiga-ui/styles/taiga-ui-global';
@import '~@taiga-ui/proprietary-core/styles/tinkoff-fonts';
@import '~@taiga-ui/proprietary-core/styles/theme-tinkoff-v2';
@import '~@taiga-ui/proprietary-core/styles/theme-tinkoff-mobile';
`;

const BEFORE_LOCAL_STYLE = `
@import '~@taiga-ui/core/styles/taiga-ui-local';
`;

const AFTER_LOCAL_STYLE = `
@import '~@taiga-ui/core/styles/taiga-ui-local';
`;

describe('replace styles', () => {
let host: UnitTestTree;
let runner: SchematicTestRunner;

beforeEach(() => {
host = new UnitTestTree(new HostTree());
runner = new SchematicTestRunner('schematics', collectionPath);

setActiveProject(createProject(host, '/', '**/**'));

createMainFiles();

saveActiveProject();
});

it('should replace with new global styles', async () => {
const tree = await runner.runSchematicAsync('updateToV3', {}, host).toPromise();

expect(tree.readContent('test/style.less')).toBe(AFTER_GLOBAL_STYLE);
expect(tree.readContent('test/app/app.template.less')).toBe(AFTER_LOCAL_STYLE);
});

afterEach(() => {
resetActiveProject();
});
});

function createMainFiles(): void {
createSourceFile(
'test/app/app.component.ts',
`
@Component({
templateUrl: './app.template.html',
styleUrls: ['./app.template.less']
})
export class AppComponent {
}
`,
);

createSourceFile('test/app/app.template.html', `<app></app>`);
createSourceFile('test/style.less', BEFORE_GLOBAL_STYLE);
createSourceFile('test/app/app.template.less', BEFORE_LOCAL_STYLE);

createAngularJson();
}
2 changes: 1 addition & 1 deletion projects/core/components/group/group-styles.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Component, ViewEncapsulation} from '@angular/core';

@Component({
template: ``,
styleUrls: [`../../styles/markup/tui-group.less`],
styleUrls: [`./group.style.less`],
encapsulation: ViewEncapsulation.None,
})
export class TuiGroupStylesComponent {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import '../taiga-ui-local';
@import '../../styles/taiga-ui-local';

.tui-group {
.createStackingContext();
Expand Down
2 changes: 1 addition & 1 deletion projects/core/styles/taiga-ui-fonts.less
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@import 'theme/fonts.css';
@import (css) url('https://fonts.googleapis.com/css2?family=Manrope:wght@500;800&display=swap');
17 changes: 0 additions & 17 deletions projects/core/styles/taiga-ui-global-old.less

This file was deleted.

7 changes: 0 additions & 7 deletions projects/core/styles/taiga-ui-local-old.less

This file was deleted.

1 change: 0 additions & 1 deletion projects/core/styles/theme/fonts.css

This file was deleted.

Loading

0 comments on commit d1a40a5

Please sign in to comment.