Skip to content

Commit

Permalink
Added the tooltip foundation component (#61)
Browse files Browse the repository at this point in the history
# Pull Request

## πŸ“– Description

<!--- Provide some background and a description of your work. -->
This adds the tooltip foundation component.

### 🎫 Issues

<!---
List and link relevant issues here using the keyword "closes"
if this PR will close an issue, eg. closes #411
-->
- Continues work on #31

## βœ… Checklist

### General

<!--- Review the list and put an x in the boxes that apply. -->

- [x] I have added tests for my changes.
- [x] I have tested my changes.
- [x] I have updated the project documentation to reflect my changes.

## ⏭ Next Steps

<!---
If there is relevant follow-up work to this PR, please list any existing issues or provide brief descriptions of what you would like to do next.
-->
- Continue adding foundation components
  • Loading branch information
janechu committed May 25, 2022
1 parent 883e176 commit eda847c
Show file tree
Hide file tree
Showing 15 changed files with 575 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Added the tooltip foundation component",
"packageName": "@microsoft/fast-cli",
"email": "7559015+janechu@users.noreply.github.com",
"dependentChangeType": "none"
}
1 change: 1 addition & 0 deletions packages/fast-cli/src/components/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export const availableTemplates = [
"text-area",
"text-field",
"toolbar",
"tooltip",
]
3 changes: 3 additions & 0 deletions packages/fast-cli/src/components/tooltip/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Description

This template is used as a tooltip component template from which a user can modify without having to do the initial scaffolding.
33 changes: 33 additions & 0 deletions packages/fast-cli/src/components/tooltip/template.pw.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect, test } from "@playwright/test";
import { execSync } from "child_process";
import {
expectedGeneratedComponentTemplateFiles,
setupComponent,
teardown,
getGeneratedComponentFiles,
getTempDir,
getTempComponentDir,
} from "../../test/helpers.js";

const uuid: string = "tooltip";
const tempDir: string = getTempDir(uuid);
const tempComponentDir: string = getTempComponentDir(uuid);

test.describe(`CLI add-foundation-component ${uuid}`, () => {
test.beforeAll(() => {
setupComponent(uuid, tempDir, tempComponentDir);
});
test.afterAll(() => {
teardown(tempDir, tempComponentDir);
});
test("should copy files from the template", () => {
expect(getGeneratedComponentFiles(tempDir)).toEqual(expectedGeneratedComponentTemplateFiles);
});
test("should be able to run the build", () => {
expect(
() => {
execSync(`cd ${tempDir} && npm run build`);
}
).not.toThrow();
});
});
8 changes: 8 additions & 0 deletions packages/fast-cli/src/components/tooltip/template/README.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { ComponentTemplateConfig } from "../../../utilities/template";

export default (config: ComponentTemplateConfig): string => `
# ${config.className}
An implementation of a [tooltip](https://w3c.github.io/aria-practices/#tooltip) web-component.
For more information on the building blocks used to create this component, please refer to https://www.fast.design/docs/components/tooltip`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { ComponentTemplateConfig } from "../../../utilities/template";

export default (config: ComponentTemplateConfig): string =>
`import { template } from "./${config.tagName}.template.js";
import { styles } from "./${config.tagName}.styles.js";
export const definition = {
baseName: "${config.tagName}",
template,
styles,
};`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { ComponentTemplateConfig } from "../../../utilities/template";

export default (config: ComponentTemplateConfig): string => `
import { expect, test } from "@playwright/test";
import { fixtureURL } from "@microsoft/fast-cli/dist/esm/utilities/playwright.js";
test.describe("${config.tagName}", () => {
const fixture = fixtureURL("${config.tagName}");
test.beforeEach(async ({ page }) => {
await page.goto(fixture);
});
test("should load the fixture URL", async ({ page }) => {
const pageUrl = page.url();
expect(pageUrl).toBe(\`http://localhost:3000/\${fixture}\`);
});
test("should contain the component in the URL", async ({ page }) => {
const element = page.locator("${config.tagName}");
await expect(element).not.toBeNull();
});
});
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { ComponentTemplateConfig } from "../../../utilities/template";

export default (config: ComponentTemplateConfig): string => `
import Template from "./fixtures/base.html";
import "./define.js";
export default {
title: "${config.tagName}",
};
export const ${config.className} = () => Template;
`;
107 changes: 107 additions & 0 deletions packages/fast-cli/src/components/tooltip/template/component.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import type { ComponentTemplateConfig } from "../../../utilities/template";

export default (config: ComponentTemplateConfig): string =>
`import { css, ElementStyles } from "@microsoft/fast-element";
import {
AnchoredRegion,
ElementDefinitionContext,
forcedColorsStylesheetBehavior,
FoundationElementDefinition,
} from "@microsoft/fast-foundation";
import {
bodyFont,
controlCornerRadius,
focusStrokeOuter,
neutralFillRest,
neutralForegroundRest,
strokeWidth,
typeRampBaseFontSize,
typeRampBaseLineHeight,
} from "@microsoft/adaptive-ui";
/**
* Styles for ${config.className}
* @public
*/
export const styles: (
context: ElementDefinitionContext,
definition: FoundationElementDefinition
) => ElementStyles = (
context: ElementDefinitionContext,
definition: FoundationElementDefinition
) => {
const anchoredRegionTag = context.tagFor(AnchoredRegion);
return css\`
:host {
contain: size;
overflow: visible;
height: 0;
width: 0;
}
.tooltip {
box-sizing: border-box;
border-radius: calc(\${controlCornerRadius} * 1px);
border: calc(\${strokeWidth} * 1px) solid \${focusStrokeOuter};
box-shadow: 0 0 0 1px \${focusStrokeOuter} inset;
background: \${neutralFillRest};
color: \${neutralForegroundRest};
padding: 4px;
height: fit-content;
width: fit-content;
font-family: \${bodyFont};
font-size: \${typeRampBaseFontSize};
line-height: \${typeRampBaseLineHeight};
white-space: nowrap;
/* TODO: a mechanism to manage z-index across components
https://github.com/microsoft/fast/issues/3813 */
z-index: 10000;
}
\${anchoredRegionTag} {
display: flex;
justify-content: center;
align-items: center;
overflow: visible;
flex-direction: row;
}
\${anchoredRegionTag}.right,
\${anchoredRegionTag}.left {
flex-direction: column;
}
\${anchoredRegionTag}.top .tooltip {
margin-bottom: 4px;
}
\${anchoredRegionTag}.bottom .tooltip {
margin-top: 4px;
}
\${anchoredRegionTag}.left .tooltip {
margin-right: 4px;
}
\${anchoredRegionTag}.right .tooltip {
margin-left: 4px;
}
\${anchoredRegionTag}.top.left .tooltip,
\${anchoredRegionTag}.top.right .tooltip {
margin-bottom: 0px;
}
\${anchoredRegionTag}.bottom.left .tooltip,
\${anchoredRegionTag}.bottom.right .tooltip {
margin-top: 0px;
}
\${anchoredRegionTag}.top.left .tooltip,
\${anchoredRegionTag}.bottom.left .tooltip {
margin-right: 0px;
}
\${anchoredRegionTag}.top.right .tooltip,
\${anchoredRegionTag}.bottom.right .tooltip {
margin-left: 0px;
}
\`.withBehaviors(
forcedColorsStylesheetBehavior(
css\`
:host([disabled]) {
opacity: 1;
}
\`
)
);
};`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { ComponentTemplateConfig } from "../../../utilities/template";

export default (config: ComponentTemplateConfig): string => `
import { Tooltip, tooltipTemplate, FoundationElementTemplate } from "@microsoft/fast-foundation";
import type { ViewTemplate } from "@microsoft/fast-element";
/**
* The template for ${config.className} component.
* @public
*/
export const template: FoundationElementTemplate<ViewTemplate<Tooltip>> = tooltipTemplate;
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { ComponentTemplateConfig } from "../../../utilities/template";

export default (config: ComponentTemplateConfig): string =>
`import { Tooltip } from "@microsoft/fast-foundation";
/**
* A class derived from the Tooltip foundation component
*/
export class ${config.className} extends Tooltip {};`
7 changes: 7 additions & 0 deletions packages/fast-cli/src/components/tooltip/template/define.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { ComponentTemplateConfig } from "../../../utilities/template";

export default (config: ComponentTemplateConfig): string =>
`import { ${config.className} } from "./${config.tagName}.js";
import { definition } from "./${config.tagName}.definition.js";
${config.className}.compose(definition);`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"packageJson": {
"dependencies": {
"@microsoft/adaptive-ui": "../adaptive-ui",
"@microsoft/fast-foundation": "^2.46.2"
}
}
}

0 comments on commit eda847c

Please sign in to comment.