-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53b72f5
commit 097a43c
Showing
17 changed files
with
345 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import Vue from 'vue'; | ||
import VueCompositionApi from '@vue/composition-api'; | ||
|
||
Vue.use(VueCompositionApi); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
|
||
/* | ||
* Transitions | ||
*/ | ||
|
||
.fade-enter-active, | ||
.fade-leave-active { | ||
transition: opacity 0.25s; | ||
} | ||
.fade-enter, | ||
.fade-leave-to { | ||
opacity: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { mount } from '@vue/test-utils'; | ||
import CToggle from './CToggle'; | ||
import CToggleBtn from './CToggleBtn'; | ||
import CToggleContent from './CToggleContent'; | ||
|
||
describe('CToggle', () => { | ||
it('wraps VNode and text but not whitespaces', () => { | ||
const wrapper = mount(CToggle, { | ||
mocks: { | ||
$chusho: { | ||
options: { | ||
components: { | ||
stack: { | ||
gaps: { | ||
1: { | ||
containerClass: 'container', | ||
itemClass: 'item', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
components: { | ||
CToggleBtn, | ||
CToggleContent, | ||
}, | ||
slots: { | ||
default: [ | ||
'<CToggleBtn>Toggle</CToggleBtn>', | ||
'<CToggleContent>Body</CToggleContent>', | ||
], | ||
}, | ||
}); | ||
|
||
console.log(wrapper.element); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import CToggle from './CToggle'; | ||
import CToggleBtn from './CToggleBtn'; | ||
import CToggleContent from './CToggleContent'; | ||
|
||
export default { | ||
title: 'Components|Toggle', | ||
component: CToggle, | ||
subcomponents: { CToggleBtn, CToggleContent }, | ||
parameters: { | ||
componentSubtitle: 'Conditionnaly display some content.', | ||
options: { | ||
componentConfig: [ | ||
{ | ||
name: 'transition', | ||
type: { summary: 'object' }, | ||
description: | ||
'Apply a common transition to all Toggles. The object can contain any Vue built-in [transition component props](https://vuejs.org/v2/api/#transition).\n\nFor example: `{ name: "fade", mode: "out-in" }`', | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
export const Default = () => ({ | ||
components: { CToggle, CToggleBtn, CToggleContent }, | ||
template: `<CToggle> | ||
<CToggleBtn variant="primary">Toggle</CToggleBtn> | ||
<CToggleContent class="bg-gray-200 px-4 py-3 rounded mt-2"> | ||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Laboriosam in, iste id nobis dolor excepturi dolore expedita vero quae. Nobis fuga cupiditate suscipit blanditiis, aliquid minima harum molestias pariatur tempora ab, libero quo maiores sapiente doloribus nihil commodi eaque accusantium praesentium! Nobis natus qui voluptate inventore molestias quisquam, consequuntur harum? | ||
</CToggleContent> | ||
</CToggle>`, | ||
}); | ||
|
||
export const UsingVModel = () => ({ | ||
components: { CToggle, CToggleBtn, CToggleContent }, | ||
data() { | ||
return { | ||
toggleOpen: true, | ||
}; | ||
}, | ||
template: `<CToggle v-model="toggleOpen"> | ||
<CToggleBtn variant="primary">{{ toggleOpen ? 'Close' : 'Open' }}</CToggleBtn> | ||
<CToggleContent class="bg-gray-200 px-4 py-3 rounded mt-2"> | ||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Laboriosam in, iste id nobis dolor excepturi dolore expedita vero quae. Nobis fuga cupiditate suscipit blanditiis, aliquid minima harum molestias pariatur tempora ab, libero quo maiores sapiente doloribus nihil commodi eaque accusantium praesentium! Nobis natus qui voluptate inventore molestias quisquam, consequuntur harum? | ||
</CToggleContent> | ||
</CToggle>`, | ||
}); | ||
|
||
UsingVModel.story = { | ||
parameters: { | ||
docs: { | ||
storyDescription: | ||
'You can bind the Toggle status with the `v-model` directive, for example to make it open by default or to programatically change its state.', | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { | ||
ref, | ||
provide, | ||
computed, | ||
InjectionKey, | ||
defineComponent, | ||
createElement, | ||
watch, | ||
Ref, | ||
} from '@vue/composition-api'; | ||
|
||
export const ToggleSymbol: InjectionKey<object> = Symbol(); | ||
|
||
export interface UseToggle { | ||
open: Readonly<Ref<boolean>>; | ||
toggle: Function; | ||
} | ||
|
||
export default defineComponent({ | ||
name: 'CToggle', | ||
|
||
model: { | ||
prop: 'open', | ||
event: 'toggle', | ||
}, | ||
|
||
props: { | ||
/** | ||
* Optionally bind the Toggle state with the parent component. | ||
*/ | ||
open: { | ||
type: Boolean, | ||
default: undefined, | ||
}, | ||
}, | ||
|
||
setup(props, { slots, emit }) { | ||
const open = ref(false); | ||
|
||
function toggle(): void { | ||
open.value = !open.value; | ||
// Update potential parent v-model value | ||
emit('toggle', open.value); | ||
} | ||
|
||
// Provide api to sub-components | ||
const api: UseToggle = { | ||
open: computed(() => open.value), | ||
toggle, | ||
}; | ||
provide(ToggleSymbol, api); | ||
|
||
// Watch potential parent v-model value changes and update state accordingly | ||
watch(() => { | ||
if (typeof props.open === 'boolean') { | ||
open.value = props.open; | ||
} | ||
}); | ||
|
||
return () => createElement('div', slots.default && slots.default()); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import CBtn from '../CBtn'; | ||
import { VNodeData } from 'vue/types/umd'; | ||
import { inject, defineComponent, createElement } from '@vue/composition-api'; | ||
import { ToggleSymbol } from './CToggle'; | ||
import { UseToggle } from './CToggle'; | ||
|
||
export default defineComponent({ | ||
name: 'CToggleBtn', | ||
|
||
setup(props, { attrs, slots, listeners }) { | ||
const toggle = inject(ToggleSymbol) as UseToggle; | ||
|
||
return () => { | ||
const componentData: VNodeData = { | ||
attrs: { | ||
...attrs, | ||
'aria-expanded': `${toggle.open.value}`, | ||
}, | ||
on: { | ||
...listeners, | ||
click: () => { | ||
toggle.toggle(); | ||
}, | ||
}, | ||
}; | ||
return createElement(CBtn, componentData, slots.default()); | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { VNode } from 'vue/types/umd'; | ||
import { inject, createElement, defineComponent } from '@vue/composition-api'; | ||
import { ToggleSymbol } from './CToggle'; | ||
import { UseToggle } from './CToggle'; | ||
import { isPlainObject } from '@/utils/objects'; | ||
|
||
export default defineComponent({ | ||
name: 'CToggleContent', | ||
|
||
props: { | ||
/** | ||
* The object can contain any Vue built-in [transition component props](https://vuejs.org/v2/api/#transition). | ||
* | ||
* For example: `{ name: "fade", mode: "out-in" }`. | ||
* | ||
* If you defined a default transition in the config and want to disable it, use `false`. | ||
*/ | ||
transition: { | ||
type: [Object, Boolean], | ||
default: null, | ||
}, | ||
}, | ||
|
||
setup(props, { slots, parent }) { | ||
const toggleConfig = parent!.$chusho?.options?.components?.toggle; | ||
const toggle = inject(ToggleSymbol) as UseToggle; | ||
let transition: object; | ||
|
||
if (isPlainObject(props.transition)) { | ||
transition = props.transition; | ||
} else if (toggleConfig && toggleConfig.transition) { | ||
transition = toggleConfig.transition; | ||
} | ||
|
||
function renderContent(): VNode | null { | ||
if (toggle.open.value) { | ||
return createElement('div', {}, slots.default()); | ||
} | ||
return null; | ||
} | ||
|
||
return () => { | ||
if (!transition) return renderContent(); | ||
return createElement('transition', { props: transition }, [ | ||
renderContent(), | ||
]); | ||
}; | ||
}, | ||
}); |
31 changes: 31 additions & 0 deletions
31
packages/chusho/src/components/CToggle/__snapshots__/CToggle.stories.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Storyshots Toggle Default 1`] = ` | ||
<div> | ||
<button | ||
aria-expanded="false" | ||
class="inline-block py-2 px-4 bg-green-200 text-green-900 rounded bg-blue-200 text-blue-900" | ||
type="button" | ||
variant="primary" | ||
> | ||
Toggle | ||
</button> | ||
<!----> | ||
</div> | ||
`; | ||
|
||
exports[`Storyshots Toggle Using V Model 1`] = ` | ||
<div> | ||
<button | ||
aria-expanded="false" | ||
class="inline-block py-2 px-4 bg-green-200 text-green-900 rounded bg-blue-200 text-blue-900" | ||
type="button" | ||
variant="primary" | ||
> | ||
Toggle | ||
</button> | ||
<!----> | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import CToggle from './CToggle'; | ||
import CToggleBtn from './CToggleBtn'; | ||
import CToggleContent from './CToggleContent'; | ||
|
||
export { CToggle, CToggleBtn, CToggleContent }; | ||
export default CToggle; |
Oops, something went wrong.