Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit a69d74c

Browse files
committed
feat(c-code): add component and write tests
1 parent ce02d1c commit a69d74c

File tree

11 files changed

+208
-0
lines changed

11 files changed

+208
-0
lines changed

packages/c-code/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# @chakra-ui/c-code
2+
3+
Code is a component used to display inline code. It is composed from the Box component with a font family of mono for displaying code
4+
5+
## Installation
6+
7+
```sh
8+
yarn add @chakra-ui/c-code
9+
# or
10+
npm i @chakra-ui/c-code
11+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<c-code> hello world! </c-code>
3+
</template>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<template>
2+
<chakra.div>
3+
<c-code
4+
v-for="(color, i) in colorSchemes"
5+
:key="i"
6+
:color-scheme="color"
7+
ml="2"
8+
>
9+
{{ color }}
10+
</c-code>
11+
</chakra.div>
12+
</template>
13+
14+
<script setup lang="ts">
15+
import { ref } from 'vue'
16+
17+
const colorSchemes = ref([
18+
'gray',
19+
'green',
20+
'red',
21+
'yellow',
22+
'orange',
23+
'purple',
24+
'teal',
25+
])
26+
</script>

packages/c-code/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './src'

packages/c-code/package.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "@chakra-ui/c-code",
3+
"description": "Chakra UI Vue | Code is a component used to display inline code. It is composed from the Box component with a font family of mono for displaying code",
4+
"version": "0.0.1-alpha.0",
5+
"main": "dist/cjs/index.js",
6+
"module": "dist/esm/index.js",
7+
"types": "dist/types/index.d.ts",
8+
"typings": "dist/types/index.d.ts",
9+
"author": "Jonathan Bakebwa <[email protected]>",
10+
"homepage": "https://github.com/chakra-ui/chakra-ui-vue-next#readme",
11+
"license": "MIT",
12+
"files": [
13+
"dist"
14+
],
15+
"exports": {
16+
".": {
17+
"require": "./dist/cjs/index.js",
18+
"default": "./dist/esm/index.js"
19+
}
20+
},
21+
"publishConfig": {
22+
"access": "public"
23+
},
24+
"repository": "https://github.com/chakra-ui/chakra-ui-vue-next/tree/master/packages/c-code",
25+
"bugs": {
26+
"url": "https://github.com/chakra-ui/chakra-ui-vue-next/issues"
27+
},
28+
"sideEffects": false,
29+
"scripts": {
30+
"build": "rimraf ./dist && concurrently yarn:build:*",
31+
"build:esm": "cross-env BABEL_ENV=esm babel src --root-mode upward --extensions .ts,.tsx -d dist/esm --source-maps",
32+
"build:cjs": "cross-env BABEL_ENV=cjs babel src --root-mode upward --extensions .ts,.tsx -d dist/cjs --source-maps",
33+
"build:types": "cross-env tsc --emitDeclarationOnly --declaration --declarationDir dist/types",
34+
"watch": "concurrently yarn:watch:*",
35+
"watch:esm": "cross-env BABEL_ENV=esm babel src --root-mode upward --extensions .ts,.tsx -d dist/esm --source-maps --watch",
36+
"watch:cjs": "cross-env BABEL_ENV=cjs babel src --root-mode upward --extensions .ts,.tsx -d dist/cjs --source-maps --watch",
37+
"watch:types": "cross-env tsc --emitDeclarationOnly --declaration --declarationDir dist/types --watch --incremental"
38+
},
39+
"dependencies": {
40+
"@chakra-ui/utils": "^1.5.0",
41+
"@chakra-ui/vue-system": "0.1.0-alpha.1"
42+
},
43+
"devDependencies": {
44+
"vue": ">=3.0.5"
45+
},
46+
"peerDependencies": {
47+
"@chakra-ui/vue-system": "0.1.0-alpha.1",
48+
"vue": "^3.0.5"
49+
},
50+
"contributors": [
51+
"Vivek Wadhwani <[email protected]>"
52+
]
53+
}

packages/c-code/src/code.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { h, defineComponent, PropType, computed } from 'vue'
2+
import {
3+
chakra,
4+
DOMElements,
5+
ThemingProps,
6+
useStyleConfig,
7+
} from '@chakra-ui/vue-system'
8+
import { filterUndefined } from '@chakra-ui/utils'
9+
10+
const CCode = defineComponent({
11+
props: {
12+
as: {
13+
type: [Object, String] as PropType<DOMElements>,
14+
default: 'code',
15+
},
16+
colorScheme: String as PropType<ThemingProps['colorScheme']>,
17+
styleConfig: String as PropType<ThemingProps['styleConfig']>,
18+
},
19+
setup(props, { slots, attrs }) {
20+
return () => {
21+
const themingProps = computed<ThemingProps>(() =>
22+
filterUndefined({
23+
colorScheme: props.colorScheme,
24+
styleConfig: props.styleConfig,
25+
})
26+
)
27+
const styles = useStyleConfig('Code', themingProps.value)
28+
29+
return h(
30+
chakra(props.as),
31+
{
32+
__css: {
33+
display: 'inline-block',
34+
verticalAlign: 'middle',
35+
fontSize: 'sm',
36+
px: '0.2em',
37+
fontFamily: 'mono',
38+
rounded: 'sm',
39+
...styles.value,
40+
},
41+
...attrs,
42+
},
43+
slots
44+
)
45+
}
46+
},
47+
})
48+
49+
export default CCode

packages/c-code/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as CCode } from './code'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`should render properly 1`] = `
4+
<DocumentFragment>
5+
<code
6+
class="css-1b9x1mh"
7+
>
8+
hello world
9+
</code>
10+
<code
11+
class="css-1hzweom"
12+
>
13+
hello world, red
14+
</code>
15+
<code
16+
class="css-bri9l4"
17+
>
18+
hello world, green
19+
</code>
20+
<code
21+
class="css-1y46pxi"
22+
>
23+
hello world, yellow
24+
</code>
25+
<code
26+
class="css-1kx42k1"
27+
>
28+
hello world, teal
29+
</code>
30+
</DocumentFragment>
31+
`;

packages/c-code/tests/c-code.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { CCode } from '../src'
2+
import { render, testA11y } from '../../test-utils/src'
3+
4+
const renderComponent = (props?: any) => {
5+
const base = {
6+
components: {
7+
CCode,
8+
},
9+
template: `
10+
<c-code>hello world</c-code>
11+
<c-code color-scheme="red"> hello world, red </c-code>
12+
<c-code color-scheme="green"> hello world, green </c-code>
13+
<c-code color-scheme="yellow"> hello world, yellow </c-code>
14+
<c-code color-scheme="teal"> hello world, teal </c-code>
15+
`,
16+
...props,
17+
}
18+
return render(base)
19+
}
20+
21+
it('should have no a11y violations', async () => {
22+
await testA11y(renderComponent())
23+
})
24+
25+
it('should render properly', () => {
26+
const { asFragment } = renderComponent()
27+
expect(asFragment()).toMatchSnapshot()
28+
})

packages/c-code/tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"include": ["src"]
4+
}

0 commit comments

Comments
 (0)