Skip to content

Commit

Permalink
feat: global nonce value via ConfigProvider (#1097)
Browse files Browse the repository at this point in the history
  • Loading branch information
romansp committed Jul 8, 2024
1 parent ee5ee67 commit 78d2327
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/content/meta/ComboboxViewport.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
{
'name': 'nonce',
'description': '',
'description': '<p>Will add <code>nonce</code> attribute to the style tag which can be used by Content Security Policy. &lt;br&gt; If omitted, inherits globally from <code>ConfigProvider</code>.</p>\n',
'type': 'string',
'required': false
}
Expand Down
6 changes: 6 additions & 0 deletions docs/content/meta/ConfigProvider.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
'required': false,
'default': '\'ltr\''
},
{
'name': 'nonce',
'description': '<p>The global <code>nonce</code> value of your application. This will be inherited by the related primitives.</p>\n',
'type': 'string',
'required': false
},
{
'name': 'scrollBody',
'description': '<p>The global scroll body behavior of your application. This will be inherited by the related primitives.</p>\n',
Expand Down
2 changes: 1 addition & 1 deletion docs/content/meta/ScrollAreaViewport.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
{
'name': 'nonce',
'description': '',
'description': '<p>Will add <code>nonce</code> attribute to the style tag which can be used by Content Security Policy. &lt;br&gt; If omitted, inherits globally from <code>ConfigProvider</code>.</p>\n',
'type': 'string',
'required': false
}
Expand Down
2 changes: 1 addition & 1 deletion docs/content/meta/SelectViewport.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
{
'name': 'nonce',
'description': '',
'description': '<p>Will add <code>nonce</code> attribute to the style tag which can be used by Content Security Policy. &lt;br&gt; If omitted, inherits globally from <code>ConfigProvider</code>.</p>\n',
'type': 'string',
'required': false
}
Expand Down
2 changes: 1 addition & 1 deletion docs/content/meta/Viewport.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
{
'name': 'nonce',
'description': '',
'description': '<p>Will add <code>nonce</code> attribute to the style tag which can be used by Content Security Policy. &lt;br&gt; If omitted, inherits globally from <code>ConfigProvider</code>.</p>\n',
'type': 'string',
'required': false
}
Expand Down
8 changes: 8 additions & 0 deletions packages/radix-vue/src/Combobox/ComboboxViewport.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<script lang="ts">
import type { PrimitiveProps } from '@/Primitive'
import { useForwardExpose } from '@/shared'
import { useNonce } from '@/shared/useNonce'
import { toRefs } from 'vue'
export interface ComboboxViewportProps extends PrimitiveProps {
/**
* Will add `nonce` attribute to the style tag which can be used by Content Security Policy. <br> If omitted, inherits globally from `ConfigProvider`.
*/
nonce?: string
}
</script>
Expand All @@ -12,6 +17,9 @@ import { Primitive } from '@/Primitive'
const props = defineProps<ComboboxViewportProps>()
const { forwardRef } = useForwardExpose()
const { nonce: propNonce } = toRefs(props)
const nonce = useNonce(propNonce)
</script>

<template>
Expand Down
10 changes: 9 additions & 1 deletion packages/radix-vue/src/ConfigProvider/ConfigProvider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createContext } from '@/shared'
interface ConfigProviderContextValue {
dir?: Ref<Direction>
scrollBody?: Ref<boolean | ScrollBodyOption>
nonce?: Ref<string | undefined>
useId?: () => string
}
Expand All @@ -23,6 +24,11 @@ export interface ConfigProviderProps {
* @type boolean | ScrollBodyOption
*/
scrollBody?: boolean | ScrollBodyOption
/**
* The global `nonce` value of your application. This will be inherited by the related primitives.
* @type string
*/
nonce?: string
/**
* The global `useId` injection as a workaround for preventing hydration issue.
*/
Expand All @@ -36,14 +42,16 @@ import { toRefs } from 'vue'
const props = withDefaults(defineProps<ConfigProviderProps>(), {
dir: 'ltr',
scrollBody: true,
nonce: undefined,
useId: undefined,
})
const { dir, scrollBody } = toRefs(props)
const { dir, scrollBody, nonce } = toRefs(props)
provideConfigProviderContext({
dir,
scrollBody,
nonce,
useId: props.useId,
})
</script>
Expand Down
9 changes: 8 additions & 1 deletion packages/radix-vue/src/ScrollArea/ScrollAreaViewport.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<script lang="ts">
import type { PrimitiveProps } from '@/Primitive'
import { useForwardExpose } from '@/shared'
import { useNonce } from '@/shared/useNonce'
export interface ScrollAreaViewportProps extends PrimitiveProps {
/**
* Will add `nonce` attribute to the style tag which can be used by Content Security Policy. <br> If omitted, inherits globally from `ConfigProvider`.
*/
nonce?: string
}
</script>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { onMounted, ref, toRefs } from 'vue'
import { injectScrollAreaRootContext } from './ScrollAreaRoot.vue'
import { Primitive } from '@/Primitive'
Expand All @@ -18,6 +22,9 @@ defineOptions({
const props = defineProps<ScrollAreaViewportProps>()
const { nonce: propNonce } = toRefs(props)
const nonce = useNonce(propNonce)
const rootContext = injectScrollAreaRootContext()
const viewportElement = ref<HTMLElement>()
Expand Down
8 changes: 7 additions & 1 deletion packages/radix-vue/src/Select/SelectViewport.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<script lang="ts">
import type { PrimitiveProps } from '@/Primitive'
import { useForwardExpose } from '@/shared'
import { useNonce } from '@/shared/useNonce'
export interface SelectViewportProps extends PrimitiveProps {
/**
* Will add `nonce` attribute to the style tag which can be used by Content Security Policy. <br> If omitted, inherits globally from `ConfigProvider`.
*/
nonce?: string
}
</script>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { onMounted, ref, toRefs } from 'vue'
import { SelectContentDefaultContextValue, injectSelectContentContext } from './SelectContentImpl.vue'
import { CONTENT_MARGIN } from './utils'
import {
Expand All @@ -17,6 +21,8 @@ import {
import { injectSelectItemAlignedPositionContext } from './SelectItemAlignedPosition.vue'
const props = defineProps<SelectViewportProps>()
const { nonce: propNonce } = toRefs(props)
const nonce = useNonce(propNonce)
const contentContext = injectSelectContentContext(SelectContentDefaultContextValue)
const alignedPositionContext
Expand Down
8 changes: 8 additions & 0 deletions packages/radix-vue/src/Viewport/Viewport.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<script lang="ts">
import type { PrimitiveProps } from '@/Primitive'
import { useForwardExpose } from '@/shared'
import { useNonce } from '@/shared/useNonce'
import { toRefs } from 'vue'
export interface ViewportProps extends PrimitiveProps {
/**
* Will add `nonce` attribute to the style tag which can be used by Content Security Policy. <br> If omitted, inherits globally from `ConfigProvider`.
*/
nonce?: string
}
</script>
Expand All @@ -12,6 +17,9 @@ import { Primitive } from '@/Primitive'
const props = defineProps<ViewportProps>()
const { forwardRef } = useForwardExpose()
const { nonce: propNonce } = toRefs(props)
const nonce = useNonce(propNonce)
</script>

<template>
Expand Down
25 changes: 25 additions & 0 deletions packages/radix-vue/src/shared/useNonce.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, it, vi } from 'vitest'
import { useNonce } from './useNonce'
import { ref } from 'vue'

describe('useNonce', () => {
vi.mock('@/ConfigProvider/ConfigProvider.vue', async () => {
return {
injectConfigProviderContext: () => {
return {
nonce: ref('global-nonce'),
}
},
}
})

it('should return global nonce value from ConfigProvider', () => {
const nonce = useNonce()
expect(nonce.value).toBe('global-nonce')
})

it('should return local nonce value when overridden', () => {
const nonce = useNonce(ref('local-nonce'))
expect(nonce.value).toBe('local-nonce')
})
})
9 changes: 9 additions & 0 deletions packages/radix-vue/src/shared/useNonce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { injectConfigProviderContext } from '@/ConfigProvider/ConfigProvider.vue'
import { type Ref, computed, ref } from 'vue'

export function useNonce(nonce?: Ref<string | undefined>) {
const context = injectConfigProviderContext({
nonce: ref(),
})
return computed(() => nonce?.value || context.nonce?.value)
}

0 comments on commit 78d2327

Please sign in to comment.