Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/old-pandas-lose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@teiler/vue': patch
---

Expose `element` ref from styled component
12 changes: 8 additions & 4 deletions packages/vue/src/Styled.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import type { HTMLElements, Sheet, StyleDefinition } from '@teiler/core'

import { DefaultTheme, insert } from '@teiler/core'
import { defineComponent, h, inject, toRaw } from 'vue'
import { defineComponent, h, inject, ref, toRaw } from 'vue'
import { context } from './ThemeProvider'
import { getStyleSheet } from './sheet'

export default function <Target extends HTMLElements, Props>(styleDefinition: StyleDefinition<Target, Props>) {
const component = defineComponent({
inheritAttrs: false,
styleDefinition,
setup() {
setup(_, { expose }) {
const styleSheet: Sheet = getStyleSheet()
const theme = inject<DefaultTheme>(context, {})

return { styleSheet, theme }
const element = ref<HTMLElement | null>(null)

expose({ element })

return { styleSheet, theme, element }
},
render() {
const slots = this.$slots
Expand All @@ -30,7 +34,7 @@ export default function <Target extends HTMLElements, Props>(styleDefinition: St
const defaultSlot = slots.default ? slots.default() : undefined

if (styleDefinition.tag) {
return h(styleDefinition.tag, { ...filtredProps, class: className }, defaultSlot)
return h(styleDefinition.tag, { ...filtredProps, class: className, ref: 'element' }, defaultSlot)
} else {
return null
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { component, global, keyframes, styled, tags } from '@teiler/core'

type ElementAttributes<Target extends HTMLElements> = Target extends keyof IntrinsicElementAttributes ? IntrinsicElementAttributes[Target] : {}

type VueRawBindings = { styleSheet: Sheet; theme: DefaultTheme }
type VueRawBindings = { styleSheet: Sheet; theme: DefaultTheme; element: HTMLElement | null }
type VueTeilerComponent<Target extends HTMLElements, Props> = TeilerComponent<Target, Props> & DefineComponent<Props, VueRawBindings, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, ElementAttributes<Target>>

const createComponent = <Target extends HTMLElements, Props>(styleDefinition: StyleDefinition<Target, Props>): VueTeilerComponent<Target, Props> => {
Expand Down
3 changes: 3 additions & 0 deletions packages/vue/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ describe('component', () => {
setup: expect.any(Function),
})

expect(wrapper.vm.element).toBeDefined()
expect(wrapper.vm.element).not.toBeNull()

expect(wrapper.html()).toBe('<div class="teiler-1r77qux t1r77qux"></div>')
})

Expand Down