Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/ui-color-picker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@instructure/ui-dom-utils": "workspace:*",
"@instructure/ui-drilldown": "workspace:*",
"@instructure/ui-form-field": "workspace:*",
"@instructure/ui-heading": "workspace:*",
"@instructure/ui-icons": "workspace:*",
"@instructure/ui-pill": "workspace:*",
"@instructure/ui-popover": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/

import { render } from '@testing-library/react'
import { render, screen } from '@testing-library/react'
import { vi } from 'vitest'
import '@testing-library/jest-dom'
import { runAxeCheck } from '@instructure/ui-axe-check'
Expand Down Expand Up @@ -76,6 +76,53 @@ describe('<ColorContrast />', () => {
})
})

describe('labelLevel prop', () => {
it('should render label as div when labelLevel is not provided', async () => {
render(<ColorContrast {...testColors} {...testLabels} />)

const heading = screen.queryByRole('heading', {
name: testLabels.label
})
expect(heading).not.toBeInTheDocument()

const labelText = screen.getByText(testLabels.label)
expect(labelText).toBeInTheDocument()
expect(labelText.tagName.toLowerCase()).toBe('div')
})

it('should render label as Heading when labelLevel is provided', async () => {
render(<ColorContrast {...testColors} {...testLabels} labelLevel="h2" />)

const heading = screen.getByRole('heading', {
name: testLabels.label,
level: 2
})
expect(heading).toBeInTheDocument()
})

it('should render correct heading level', async () => {
const { rerender } = render(
<ColorContrast {...testColors} {...testLabels} labelLevel="h3" />
)

let heading = screen.getByRole('heading', {
name: testLabels.label,
level: 3
})
expect(heading).toBeInTheDocument()

rerender(
<ColorContrast {...testColors} {...testLabels} labelLevel="h1" />
)

heading = screen.getByRole('heading', {
name: testLabels.label,
level: 1
})
expect(heading).toBeInTheDocument()
})
})

describe('should calculate contrast correctly', () => {
it('on opaque colors', async () => {
const color1 = '#fff'
Expand Down
14 changes: 11 additions & 3 deletions packages/ui-color-picker/src/ColorContrast/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import { withStyle } from '@instructure/emotion'

import { Text } from '@instructure/ui-text'
import { Heading } from '@instructure/ui-heading'
import { Pill } from '@instructure/ui-pill'

import ColorIndicator from '../ColorIndicator'
Expand Down Expand Up @@ -191,6 +192,7 @@ class ColorContrast extends Component<ColorContrastProps, ColorContrastState> {
const {
styles,
label,
labelLevel,
normalTextLabel,
largeTextLabel,
graphicsTextLabel
Expand All @@ -211,9 +213,15 @@ class ColorContrast extends Component<ColorContrastProps, ColorContrastState> {
data-cid="ColorContrast"
>
<div css={styles?.label}>
<Text weight="bold" as="div">
{label}
</Text>
{labelLevel ? (
<Heading as={labelLevel} level="reset">
{label}
</Heading>
) : (
<Text weight="bold" as="div">
{label}
</Text>
)}
</div>
<Text size="x-large">{contrast}:1</Text>
{this.renderPreview()}
Expand Down
8 changes: 8 additions & 0 deletions packages/ui-color-picker/src/ColorContrast/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
* SOFTWARE.
*/

import React from 'react'
import type { WithStyleProps, ComponentStyle } from '@instructure/emotion'
import type {
OtherHTMLAttributes,
ColorContrastTheme
} from '@instructure/shared-types'

type HeadingLevel<U extends keyof React.JSX.IntrinsicElements> = U

type ColorContrastOwnProps = {
/**
* Provides a reference to the component's underlying html element.
Expand All @@ -53,6 +56,10 @@ type ColorContrastOwnProps = {
* Label of the component
*/
label: string
/**
* The heading level for the label. If provided, the label will be rendered as a `<Heading />` instead of `<Text />`.
*/
labelLevel?: HeadingLevel<'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'>
/**
* Text of the second check (Suggested english text: Large text)
*/
Expand Down Expand Up @@ -154,6 +161,7 @@ const allowedProps: AllowedPropKeys = [
'graphicsTextLabel',
'withoutColorPreview',
'label',
'labelLevel',
'largeTextLabel',
'normalTextLabel',
'secondColor',
Expand Down
9 changes: 7 additions & 2 deletions packages/ui-color-picker/src/ColorContrast/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import type { ColorContrastTheme } from '@instructure/shared-types'
import type { ColorContrastProps } from './props'
/**
* ---
* private: true
Expand All @@ -33,7 +34,10 @@ import type { ColorContrastTheme } from '@instructure/shared-types'
* @param {Object} state the state of the component, the style is applied to
* @return {Object} The final style object, which will be used in the component
*/
const generateStyle = (componentTheme: ColorContrastTheme) => {
const generateStyle = (
componentTheme: ColorContrastTheme,
props: ColorContrastProps
) => {
const statusDescriptionStyle = (pass: boolean) => ({
label: pass
? 'colorContrast__successDescription'
Expand Down Expand Up @@ -94,7 +98,8 @@ const generateStyle = (componentTheme: ColorContrastTheme) => {
},
label: {
label: 'colorContrast__label',
marginBottom: componentTheme.labelBottomMargin
marginBottom: componentTheme.labelBottomMargin,
...(props.labelLevel && { fontWeight: 'bold' })
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/ui-color-picker/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
{ "path": "../ui-buttons/tsconfig.build.json" },
{ "path": "../ui-color-utils/tsconfig.build.json" },
{ "path": "../ui-form-field/tsconfig.build.json" },
{ "path": "../ui-heading/tsconfig.build.json" },
{ "path": "../ui-icons/tsconfig.build.json" },
{ "path": "../ui-pill/tsconfig.build.json" },
{ "path": "../ui-popover/tsconfig.build.json" },
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading